Java Programming: Control Flow
Introduction
Control flow statements in Java allow you to dictate the order in which statements are executed. This is critical for implementing logic in your programs. The primary control flow statements in Java include:
- Conditional Statements
- Looping Statements
- Branching Statements
Conditional Statements
Conditional statements allow you to execute specific sections of code based on specific conditions. Java supports several conditional statements:
if Statement
The if statement executes a block of code if a specified condition is true.
if (condition) { // code to be executed if condition is true }
Example:
int number = 10; if (number > 0) { System.out.println("The number is positive."); }
if-else Statement
The if-else statement executes one block of code if a condition is true and another block of code if it is false.
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
Example:
int number = -10; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); }
if-else-if Ladder
The if-else-if ladder allows you to check multiple conditions sequentially.
if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if all conditions are false }
Example:
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."); }
switch Statement
The switch statement allows you to execute one block of code out of many, based on the value of a variable.
switch (variable) { case value1: // code to be executed if variable == value1 break; case value2: // code to be executed if variable == value2 break; // you can have any number of case statements default: // code to be executed if variable does not match any case }
Example:
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"); }
Looping Statements
Looping statements allow you to execute a block of code repeatedly. Java supports several types of loops:
for Loop
The for loop is used to execute a block of code a specific number of times.
for (initialization; condition; update) { // code to be executed }
Example:
for (int i = 0; i < 5; i++) { System.out.println("Iteration: " + i); }
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
while Loop
The while loop is used to repeatedly execute a block of code as long as a specified condition is true.
while (condition) { // code to be executed }
Example:
int i = 0; while (i < 5) { System.out.println("Iteration: " + i); i++; }
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
do-while Loop
The do-while loop is similar to the while loop but guarantees that the code block is executed at least once.
do { // code to be executed } while (condition);
Example:
int i = 0; do { System.out.println("Iteration: " + i); i++; } while (i < 5);
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Branching Statements
Branching statements allow you to break the flow of a loop or a switch statement. Java supports several branching statements:
break Statement
The break statement is used to terminate the loop or switch statement.
for (int i = 0; i < 10; i++) { if (i == 5) { break; } System.out.println("Iteration: " + i); }
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
continue Statement
The continue statement skips the current iteration of the loop and proceeds to the next iteration.
for (int i = 0; i < 10; i++) { if (i == 5) { continue; } System.out.println("Iteration: " + i); }
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 9
return Statement
The return statement is used to exit from a method and optionally return a value.
public int add(int a, int b) { return a + b; }
Example:
public class Main { public static void main(String[] args) { Main obj = new Main(); int result = obj.add(5, 3); System.out.println("Sum: " + result); } public int add(int a, int b) { return a + b; } }