Home RoadMap Blog Contact us Learn

2.Core Java Concepts: Control flow Statements

Core Java Concepts: Control Flow Statements

Controlling the flow of execution is essential. Java offers various control flow statements that allow us to decide which part of the code should execute based on certain conditions or how many times a piece of code should run. Let's explore the most commonly used control flow statements in Java.

1. Loops in Java

Loops allow us to execute a block of code repeatedly. There are three main types of loops in Java:

  • While Loop: The while loop continues to execute as long as the condition inside the parentheses is true.
    int count = 0;
    while (count < 5) {
        System.out.println("Count: " + count);
        count++;
    }

    In the example above, the loop will print the value of count from 0 to 4. Once the condition count < 5 becomes false, the loop stops.

  • For Loop: The for loop is used when the number of iterations is known. It consists of three parts: initialization, condition, and update.
    for (int i = 0; i < 5; i++) {
        System.out.println("i: " + i);
    }

    This will print i values from 0 to 4. The loop runs until the condition i < 5 is false.

  • For-each (Enhanced For Loop): The enhanced for loop is used to iterate over arrays or collections. It simplifies traversing through elements without the need for an index.
    int[] numbers = {1, 2, 3, 4, 5};
    for (int num : numbers) {
        System.out.println("Number: " + num);
    }

    In this example, the loop will print each number from the array numbers without needing to keep track of the index.

Quick Exercise: Use a for loop to print all even numbers from 0 to 10. Then, try using a while loop to do the same task.

2. Conditional Statements in Java

Conditional statements allow us to make decisions in our code based on certain conditions. The most commonly used conditional statements are:

  • If Statement: The if statement checks a condition, and if it's true, the code block inside the if is executed.
    int age = 18;
    if (age >= 18) {
        System.out.println("You are eligible to vote.");
    }

    In this example, if age is greater than or equal to 18, the message "You are eligible to vote" will be printed.

  • If-Else Statement: The if-else statement provides an alternative path of execution when the condition is false.
    int age = 16;
    if (age >= 18) {
        System.out.println("You are eligible to vote.");
    } else {
        System.out.println("You are not eligible to vote.");
    }

    If age is less than 18, the message "You are not eligible to vote" will be printed.

  • Switch Statement: The switch statement is used to select one of many blocks of code to be executed based on the value of an expression.
    int day = 2;
    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");
    }

    In this example, since day is 2, the output will be "Tuesday". The break statement ensures the switch exits after a match is found.

Quick Exercise: Use a switch statement to print the name of a month based on its number (e.g., 1 for January, 2 for February, etc.).

Summary

We've now covered control flow statements like loops (while, for, for-each) and conditional statements (if, if-else, switch). These are crucial for making your programs dynamic and flexible. Practice using these statements to gain confidence!

Control flow statements in java

Recent Posts