Java Control Flow Statements
Control flow statements in Java are used to determine the order in which statements are executed in a program. They allow you to make decisions, repeat actions, and perform different actions based on conditions.
1. if
Statement
The if statement is used to test a condition. If the condition is true, the block of code inside the if statement is executed.
public class Main {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
2. if-else
Statement
The if-else statement executes one block of code if the condition is true, and another block of code if the condition is false.
public class Main {
public static void main(String[] args) {
int number = -10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
}
}
3. if-else if-else
Statement
The if-else if-else statement allows you to test multiple conditions.
public class Main {
public static void main(String[] args) {
int number = 0;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
4. switch Statement
The switch statement allows you to select one of many code blocks to be executed.
public class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
5. while Loop
The while loop repeats a block of code as long as a specified condition is true.
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}
6. do-while Loop
The do-while loop is similar to the while loop, but it executes the block of code at least once before checking the condition.
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}
7. for Loop
The for loop is used to repeat a block of code a certain number of times.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
8. for-each Loop
The for-each loop is used to iterate through elements of a collection or array.
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
}
}
9. break Statement
The break statement is used to exit a loop or switch statement prematurely.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
}
}
10. continue Statement
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}