Control flow statements in Java allow you to control the execution of your program based on certain conditions or loops. This tutorial covers the three main types of control flow statements: conditional statements (if, else, switch), loops (for, while, do-while), and branching statements (break, continue, return).
Java Control Flow Tutorial
By the end of this tutorial, you'll understand how to use control flow statements to make decisions, repeat tasks, and manage the flow of your Java programs.
Conditional Statements
Conditional statements allow you to execute different blocks of code based on specific conditions.
- if Statement:
- Executes a block of code if a condition is
true
. - Syntax:
if (condition) { // Code to execute if condition is true }
- Example:
int x = 10; if (x > 5) { System.out.println("x is greater than 5"); }
- Executes a block of code if a condition is
- if-else Statement:
- Executes one block of code if the condition is
true
and another block if it isfalse
. - Syntax:
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
- Example:
int x = 3; if (x > 5) { System.out.println("x is greater than 5"); } else { System.out.println("x is less than or equal to 5"); }
- Executes one block of code if the condition is
- if-else-if Ladder:
- Used to test multiple conditions.
- Syntax:
if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if all conditions are false }
- Example:
int x = 7; if (x > 10) { System.out.println("x is greater than 10"); } else if (x > 5) { System.out.println("x is greater than 5 but less than or equal to 10"); } else { System.out.println("x is less than or equal to 5"); }
- switch Statement:
- Used to select one of many code blocks to execute based on the value of a variable.
- Syntax:
switch (variable) { case value1: // Code to execute if variable == value1 break; case value2: // Code to execute if variable == value2 break; default: // Code to execute if variable doesn't 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; default: System.out.println("Invalid day"); }
Loops
Loops allow you to repeat a block of code multiple times.
- for Loop:
- Used when the number of iterations is known.
- Syntax:
for (initialization; condition; update) { // Code to execute }
- Example:
for (int i = 0; i < 5; i++) { System.out.println("i = " + i); }
- while Loop:
- Used when the number of iterations is not known and depends on a condition.
- Syntax:
while (condition) { // Code to execute }
- Example:
int i = 0; while (i < 5) { System.out.println("i = " + i); i++; }
- do-while Loop:
- Similar to the
while
loop, but the code block is executed at least once. - Syntax:
do { // Code to execute } while (condition);
- Example:
int i = 0; do { System.out.println("i = " + i); i++; } while (i < 5);
- Similar to the
Branching Statements
Branching statements allow you to control the flow of loops and switch statements.
- break:
- Used to exit a loop or switch statement.
- Example:
for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exit the loop when i == 5 } System.out.println("i = " + i); }
- continue:
- Used to skip the current iteration of a loop and proceed to the next iteration.
- Example:
for (int i = 0; i < 5; i++) { if (i == 2) { continue; // Skip iteration when i == 2 } System.out.println("i = " + i); }
- return:
- Used to exit a method and optionally return a value.
- Example:
public int add(int a, int b) { return a + b; // Return the sum of a and b }
This tutorial covered the essential control flow statements in Java. Practice using these statements to build dynamic and efficient programs.