Control flow and Decision Making statements

Introduction to Control Structures

Control structures are fundamental elements in programming that dictate the flow of control through a program. They enable conditional execution of code segments, looping through data, and branching logic.

If-Else Statements

Introduction to If-Else Statements

The if-else statement is used to execute a block of code based on a condition. If the condition evaluates to true, the block of code inside the if statement is executed. Otherwise, the block inside the else statement is executed.

Syntax

Java
if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Example: If-Else Statement

This example demonstrates a simple if-else statement.

Java
public class IfElseExample {
    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.");
        }
    }
}

Explanation

  • The variable number is checked to see if it is greater than 0.
  • If number > 0 is true, “The number is positive.” is printed.
  • If number > 0 is false, “The number is not positive.” is printed.

Else-If Ladder

When multiple conditions need to be checked, else-if can be used.

Syntax

Java
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 both conditions are false
}

Example: Else-If Ladder

This example demonstrates the use of an else-if ladder.

Java
public class ElseIfExample {
    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.");
        }
    }
}

Explanation

  • The variable number is checked to see if it is greater than, less than, or equal to 0.
  • Based on the value of number, the corresponding message is printed.

Switch Statement

Introduction to Switch Statement

The switch statement is used to execute one block of code from multiple options based on the value of an expression.

Syntax

Java
switch (expression) {
    case value1:
        // code to be executed if expression == value1
        break;
    case value2:
        // code to be executed if expression == value2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if expression doesn't match any case
}

Example: Switch Statement

This example demonstrates the use of a switch statement.

Java
public class SwitchExample {
    public static void main(String[] args) {
        int day = 3;
        String dayName;

        switch (day) {
            case 1:
                dayName = "Sunday";
                break;
            case 2:
                dayName = "Monday";
                break;
            case 3:
                dayName = "Tuesday";
                break;
            case 4:
                dayName = "Wednesday";
                break;
            case 5:
                dayName = "Thursday";
                break;
            case 6:
                dayName = "Friday";
                break;
            case 7:
                dayName = "Saturday";
                break;
            default:
                dayName = "Invalid day";
                break;
        }

        System.out.println("The day is " + dayName);
    }
}

Explanation

  • The switch statement evaluates the value of day.
  • Based on the value, the corresponding case block is executed.
  • The break statement exits the switch block to prevent fall-through.
  • The default case handles any values not matched by the case statements.

Loops

Introduction to Loops

Loops are used to execute a block of code repeatedly until a specified condition is met. Java supports several types of loops: for, while, and do-while.

For Loop

The for loop is used when the number of iterations is known beforehand.

Syntax

Java
for (initialization; condition; increment/decrement) {
    // code to be executed
}

Example: For Loop

This example demonstrates the use of a for loop.

Java
public class ForLoopExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Iteration: " + i);
        }
    }
}

Explanation

  • The loop initializes i to 1.
  • The condition i <= 5 is checked before each iteration.
  • After each iteration, i is incremented by 1.

While Loop

The while loop is used when the number of iterations is not known beforehand.

Syntax

Java
while (condition) {
    // code to be executed
}

Example: While Loop

This example demonstrates the use of a while loop.

Java
public class WhileLoopExample {
    public static void main(String[] args) {
        int i = 1;

        while (i <= 5) {
            System.out.println("Iteration: " + i);
            i++;
        }
    }
}

Explanation

  • The loop starts with i initialized to 1.
  • The condition i <= 5 is checked before each iteration.
  • After each iteration, i is incremented by 1.

Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the code inside the loop is executed at least once.

Syntax

Java
do {
    // code to be executed
} while (condition);

Example: Do-While Loop

This example demonstrates the use of a do-while loop.

Java
public class DoWhileLoopExample {
    public static void main(String[] args) {
        int i = 1;

        do {
            System.out.println("Iteration: " + i);
            i++;
        } while (i <= 5);
    }
}

Explanation

  • The loop starts with i initialized to 1.
  • The code inside the do block is executed before the condition is checked.
  • The condition i <= 5 is checked after each iteration.
  • After each iteration, i is incremented by 1.

Conclusion

Java control structures such as if-else, switch, and loops (for, while, do-while) are essential for controlling the flow of a program. By mastering these structures, you can write efficient and logical code to solve a wide range of programming problems. Practice these control structures with different examples to gain a solid understanding and improve your programming skills.

Scroll to Top