Control flow statements in Java allow you to control the flow of your program's execution based on certain conditions. These statements include if-else, switch, and loops like for, while, and do-while. Let's explore each with simple examples:
true or false.int x = 10;
if (x > 0) {
System.out.println("x is positive");
} else {
System.out.println("x is not positive");
}matching cases.int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Invalid day";
}
System.out.println("Day is: " + dayName);each time.for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}true.int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}similar to the while loop but guarantees the code block will execute at least once before evaluating the condition.int i = 1;
do {
System.out.println("Count: " + i);
i++;
} while (i <= 5);