
Java Control Flow Statement
In Java, control flow statements determine the order in which code executes. The main control flow statements are conditional statements (if-else, switch) and loops (for, while, do-while), which allow you to create logic based on conditions and repeat code as needed.
1. Conditional Statements
1.1 if-else Statement
The if-else statement checks a condition and executes code based on whether the condition is true or false.
if: Executes code if a condition is true.
else: Executes code if the if condition is false.
else if: Used for multiple conditions, providing additional checks.
Syntax:
Example:
Explanation:
If score is 90 or more, it prints "Grade: A".
If score is between 75 and 89, it prints "Grade: B".
If score is less than 75, it prints "Grade: C".
1.2 switch Statement
The switch statement is useful when there are multiple values to check for a single variable. It’s a good alternative to a series of if-else statements when comparing the same variable to multiple values.
Syntax:
Explanation:
The variable day is compared to each case value.
When day is 3, it matches case 3, setting dayName to "Wednesday".
The break statement prevents fall-through (executing subsequent cases).
If none of the cases match, the default block executes.
2. Loops
Loops allow code to repeat as long as certain conditions are met.
2.1 for Loop
The for loop is useful when you know in advance how many times you want to iterate.
Syntax:
for (initialization; condition; update) {
// Code block to execute
}Example:
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++){
System.out.println("Iteration "Â + i);
}
}
}Explanation:
Initialization: int i = 1 initializes the loop counter i to 1.
Condition: i <= 5 checks if i is less than or equal to 5. If true, the loop executes; otherwise, it ends.
Update: i++ increments i by 1 after each iteration.
The loop runs five times, printing "Iteration 1" through "Iteration 5".
2.2 while Loop
The while loop executes as long as its condition is true. It’s often used when the number of iterations isn’t known in advance.
Syntax:
while (condition) { // Code block to execute }Example:
public class WhileLoopExample { public static void main(String[] args) {
int count = 1; while (count <= 3) {
System.out.println("Count is: "Â + count);
count++;
}
}
}Explanation:
The loop checks if count is less than or equal to 3.
If true, it executes the code block and increments count.
When count becomes 4, the condition is false, and the loop exits.
2.3 do-while Loop
The do-while loop is similar to while, but it guarantees at least one execution of the code block because the condition is checked after the block.
Syntax:
do { // Code block to execute } while (condition);Example:
public class DoWhileExample {
public static void main(String[] args) {
int num = 1; do {
System.out.println("Number: "Â + num);
num++;
}
while (num <= 3);
}
}Explanation:
The loop starts by printing "Number: 1".
num is incremented, and the loop checks if num <= 3.
It continues until num becomes 4, then exits.
3. Nested Loops
You can place loops inside other loops, known as nested loops. They’re often used for working with multidimensional data.
Example:
public class NestedLoopExample { public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i = "Â + i + ", j = "Â + j);
}
}
}
}Explanation:
The outer loop (with i) runs 3 times, controlling the rows.
For each i, the inner loop (with j) runs 2 times.
The output displays all combinations of i and j.
4. Practical Example: Combining if-else and Loops
Let’s create a program that finds whether a number is prime. A prime number has no divisors other than 1 and itself.
Explanation:
if condition: Numbers less than 2 are not prime.
for loop: Checks divisibility from 2 up to number / 2.
if inside loop: If number % i == 0, number is not prime, and the loop breaks.
Final if-else: Prints whether the number is prime or not.
Summary
if-else and switch statements control which block of code executes based on conditions.
for, while, and do-while loops allow code to repeat as long as conditions are met.
Nested loops and conditional checks within loops help handle complex scenarios like multidimensional arrays or prime number checks.
These control flow statements are fundamental to creating flexible and efficient Java applications. Let me know if you’d like more examples or further clarification on any part!