EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Flow Control in java

Decision-making in programming is like making decisions in real life. When coding, we often need to execute certain blocks of code only if specific conditions are met.

Java provides several control statements to control the flow of a program based on conditions. These statements direct the flow of execution, allowing it to branch and continue based on the program’s state.

Java’s Selection Statements:

  • if
  • if-else
  • nested-if
  • if-else-if
  • switch-case
  • jump (break, continue, return)

1. if Statement:

The simplest decision-making statement. It is used to decide whether a certain block of code will be executed or not, depending on whether the condition is true.

Syntax:

if(condition) {
   // Statements to execute if the condition is true
}

Here, the condition evaluates to either true or false. If true, the block of statements inside the if block is executed.

If we do not use curly braces ({}), only the first statement after the if will be considered part of the if block.

Example:

class IfDemo {
    public static void main(String args[]) {
        int i = 10;

        if (i < 15)
            System.out.println("Inside If block"); // Part of if block
        System.out.println("10 is less than 15");  // Outside of if block
        System.out.println("I am not in if block");
    }
}

Output:

Inside If block
10 is less than 15
I am not in if block

2. if-else Statement:

The if statement alone can execute code when a condition is true. But what if we want to execute something else when the condition is false? This is where the else statement comes in.

Syntax:

if (condition) {
    // Executes if condition is true
} else {
    // Executes if condition is false
}

Example:

class IfElseDemo {
    public static void main(String args[]) {
        int i = 10;

        if (i < 15)
            System.out.println("i is smaller than 15");
        else
            System.out.println("i is greater than 15");
    }
}

Output:

i is smaller than 15

3. nested-if Statement:

nested-if is an if statement inside another if. This allows you to perform more complex decision-making scenarios.

Syntax:

if (condition1) {
   if (condition2) {
      // Executes when both conditions are true
   }
}

Example:

class NestedIfDemo {
    public static void main(String args[]) {
        int i = 10;

        if (i == 10 || i < 15) {
            if (i < 15)
                System.out.println("i is smaller than 15");

            if (i < 12)
                System.out.println("i is smaller than 12 too");
        } else {
            System.out.println("i is greater than 15");
        }
    }
}

Output:

i is smaller than 15
i is smaller than 12 too

4. if-else-if Ladder:

The if-else-if ladder allows you to test multiple conditions. The conditions are evaluated from top to bottom, and as soon as one of them is true, its corresponding block is executed, and the rest are skipped.

Syntax:

if (condition1) {
    // Executes if condition1 is true
} else if (condition2) {
    // Executes if condition2 is true
} else {
    // Executes if none of the above conditions are true
}

Example:

class IfElseIfDemo {
    public static void main(String args[]) {
        int i = 20;

        if (i == 10)
            System.out.println("i is 10");
        else if (i == 15)
            System.out.println("i is 15");
        else if (i == 20)
            System.out.println("i is 20");
        else
            System.out.println("i is not present");
    }
}

Output:

i is 20

5. switch-case Statement:

The switch statement allows you to branch execution based on the value of an expression. It’s like having multiple if-else blocks for a single expression.

Syntax:

switch (expression) {
  case value1:
    // Code to be executed if expression equals value1
    break;
  case value2:
    // Code to be executed if expression equals value2
    break;
  // Add more cases as needed
  default:
    // Code to be executed if none of the cases match
}

Example:

class SwitchDemo {
    public static void main(String[] args) {
        int num = 20;
        switch (num) {
          case 5:
            System.out.println("It is 5");
            break;
          case 10:
            System.out.println("It is 10");
            break;
          case 15:
            System.out.println("It is 15");
            break;
          case 20:
            System.out.println("It is 20");
            break;
          default:
            System.out.println("Not present");
        }
    }
}

Output:

It is 20

6. Jump Statements:

Java has three main jump statements:

  • break: Used to exit a loop or a switch statement.
  • continue: Skips the remaining code in the current iteration of a loop and starts the next iteration.
  • return: Exits from the current method and passes control back to the caller.

Example of continue:

class ContinueDemo {
    public static void main(String args[]) {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0)
                continue;

            System.out.print(i + " ");
        }
    }
}

Output:

1 3 5 7 9
End of lesson.