Control Flow in Java
Chapter 5: Control Flow in Java
Control flow in Java refers to the order in which instructions, statements, and function calls are executed or evaluated. Java provides several mechanisms to control the flow of execution through the program:
🧩 1. Conditional Statements
Conditional statements execute different blocks of code based on certain conditions.
🔸 if Statement
int number = 10;
if (number > 5) {
System.out.println("Number is greater than 5");
}
🔸 if-else Statement
int number = 4;
if (number > 5) {
System.out.println("Greater than 5");
} else {
System.out.println("Less than or equal to 5");
}
🔸 if-else-if Ladder
int score = 85;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 80) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
🧩 2. switch Statement
The switch
statement allows a variable to be tested for equality against multiple values.
int day = 2;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid day");
}
🔁 3. Looping Statements
Loops are used to execute a block of code repeatedly until a condition is met.
🔸 while Loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
🔸 do-while Loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
🔸 for Loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
🔸 Enhanced for Loop (for-each)
int[] numbers = {1, 2, 3, 4};
for (int num : numbers) {
System.out.println(num);
}
🛑 4. Control Flow Keywords
🔸 break Statement
Used to exit a loop or switch block prematurely.
for (int i = 0; i < 10; i++) {
if (i == 5) break;
System.out.println(i);
}
🔸 continue Statement
Skips the current iteration and continues with the next iteration of the loop.
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.println(i);
}
🔸 return Statement
Ends the execution of the current method and optionally returns a value.
public int sum(int a, int b) {
return a + b;
}
📘 Chapter Summary
- if/else: Used for decision making
- switch: Multiple condition branching
- Loops: Repetitive execution
- break/continue: Modify loop behavior
- return: Exit from method
❓ FAQs with Answers
Q1: What is the difference between if-else and switch?
A: if-else
can handle ranges and complex conditions, while switch
is used for fixed values. Use switch for better readability with many fixed cases.
Q2: What is the use of break in loops?
A: break
immediately exits the loop or switch statement where it is present, skipping the remaining iterations.
Q3: What is the difference between while and do-while loop?
A: while
checks condition before execution, do-while
executes at least once regardless of the condition.
Q4: What is the use of continue?
A: continue
skips the current iteration and proceeds to the next loop cycle.
Q5: When should we use return?
A: return
is used to exit a method and optionally return a value to the calling method.
Q6: What is an enhanced for loop in Java?
A: It is a simplified for loop used for arrays or collections where index manipulation is not needed.
Q7: Can break be used outside of loops or switch?
A: No, break
must be used inside loops or switch statements only.
📌 Related Links: Complete Java Course
- Chapter 1: Introduction to Java
- Chapter 2: Java Environment Setup
- Chapter 3: Variables and Data Types
- Chapter 4: Operators in Java
- Chapter 5: Control Statements
- Chapter 6: Loops in Java
- Chapter 7: Arrays and Strings
- Chapter 8: Object-Oriented Programming (OOP)
- Chapter 9: Inheritance and Polymorphism
- Chapter 10: Exception Handling
- Chapter 11: Multithreading
- Chapter 12: File Handling in Java
- Chapter 13: Applets and GUI (AWT/Swing)
- Chapter 14: Java Project Ideas & Practice Questions
Comments
Post a Comment