Java Control Statements | Control Flow in Java

Java provides three types of control flow statements.

  1. Decision Making statements
    • if statements
    • switch statement
  2. Loop statements
    • do while loop
    • while loop
    • for loop
    • for-each loop
  3. Jump statements
    • break statement
    • continue statement

Control statements in Java are the backbone of program logic. They allow you to control the order in which statements are executed, enabling you to create programs that make decisions, repeat actions, and handle different scenarios. They are broadly categorized into:

  1. Decision-Making Statements: These statements allow your program to execute different blocks of code based on certain conditions.

  2. Looping Statements: These statements allow you to repeat a block of code multiple times.

  3. Jump Statements: These statements allow you to transfer control to a specific part of your program.

1. Decision-Making Statements:

  • if statement: The simplest decision-making statement. It executes a block of code if a condition is true.

Java

if (age >= 18) {
    System.out.println("You are an adult.");
}
  • if-else statement: Executes one block of code if a condition is true and another block if it is false.

Java

if (temperature > 30) {
    System.out.println("It's hot!");
} else {
    System.out.println("It's not too hot.");
}
  • if-else if-else statement: Allows you to check multiple conditions in sequence.

Java

if (score >= 90) {
    grade = 'A';
} else if (score >= 80) {
    grade = 'B';
} else if (score >= 70) {
    grade = 'C';
} else {
    grade = 'D';
}
  • switch statement: Provides a more concise way to handle multiple conditions based on the value of a single variable.

Java

switch (day) {
    case "Monday":
        System.out.println("It's Monday.");
        break; // Important: Don't forget the break!
    case "Tuesday":
        System.out.println("It's Tuesday.");
        break;
    default:
        System.out.println("It's some other day.");
}

2. Looping Statements:

  • for loop: Used when you know the number of iterations in advance.

Java

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
  • while loop: Used when the number of iterations is not known in advance and depends on a condition.

Java

int i = 0;
while (i < 10) {
    System.out.println(i);
    i++;
}
  • do-while loop: Similar to while, but the loop body executes at least once before the condition is checked.

Java

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 10);
  • Enhanced for loop (for-each loop): A simplified way to iterate over arrays and collections.

Java

int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
    System.out.println(number);
}

3. Jump Statements:

  • break statement: Used to terminate a loop or a switch statement.

Java

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i is 5
    }
    System.out.println(i);
}
  • continue statement: Used to skip the current iteration of a loop and proceed to the next iteration.

Java

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    System.out.println(i); // Print only odd numbers
}
  • return statement: Used to exit a method (discussed in detail in the previous response).

Java Control Statements | Control Flow in Java

Example: Combining Control Statements:

Java

public class ControlFlowExample {
    public static void main(String[] args) {
        int[] scores = {85, 92, 78, 65, 95, 70};
        char grade;

        for (int score : scores) {
            if (score >= 90) {
                grade = 'A';
            } else if (score >= 80) {
                grade = 'B';
            } else if (score >= 70) {
                grade = 'C';
            } else {
                grade = 'D';
            }
            System.out.println("Score: " + score + ", Grade: " + grade);

            if (grade == 'D') {
                System.out.println("Needs improvement!");
            }
        }
    }
}

This example demonstrates how you can combine different control statements (for, if-else if) to create more complex logic.

Related Posts