Java Control Flow Statements

Types, Syntax, and Examples

Java Control Flow Statements

What is a control flow statement?

Control flow statements in Java regulate the execution of statements within a program. They determine the order in which statements are executed based on certain conditions or loops.

  1. Basic Control Flow Structure

Sequence:

In a sequence, Statement executes in the way they have written, line by line.

int a = 5;
int b = 10;
int sum = a + b; // Statements are executed in sequence
System.out.println("The sum is: " + sum);

Selection:

if statement: it will execute the code when the condition in if(condition) is true.

if (condition) {
    // Code to be executed if the condition is true
}
//example
int x = 7;

if (x > 5) {
    System.out.println("x is greater than 5");// it will excecute when x is greater then 5
}

Switch Statement: Theswitch statement is used for multi-way branching based on the value of an expression. It provides an alternative to a series ofif-else if statements with multiple possible execution paths.

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;
    // additional cases as needed
    default:
        // Code to be executed if none of the cases match
}

Example

int dayOfWeek = 3;

switch (dayOfWeek) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    // additional cases for other days
    default:
        System.out.println("Invalid day");
}
  1. Repeat Control Flow Statement

Loops

Repeat the execution of the code until the conditions are met.

While Loop: The while loop repeatedly executes a block of code as long as a specified condition is true.

Syntax:

while (condition) {
    // Code to be executed while the condition is true
}

Example:

int count = 0;

while (count < 5) {
    System.out.println("Count: " + count);
    count++;
}

Do While: The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once, even if the condition is initially false.

Syntax:

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

Example:

int count = 0;

do {
    System.out.println("Count: " + count);
    count++;
} while (count < 5);

For loops: It is crucial. Thefor loop is a compact way to express loops. It includes an initialization, a condition, and an iteration expression, all within the loop header.

Syntax:

for (initialization; condition; iteration) {
    // Code to be executed in each iteration
}

Example:

for (int i = 0; i < 5; i++) {
    System.out.println("i: " + i);
}

Enhanced for loop (foreach): The enhanced for loop (or foreach loop) is a simplified version of the for loop designed for iterating over arrays or collections. One thing you should remember about the "for-each" loop is that iterate over the entire array or collection. If you don't want some elements of a collection or array to be iterated,in that case, a for loop is suitable.

Syntax:

for (elementType element : array) {
    // Code to be executed for each element in the array
}

Example:

int[] numbers = {1, 2, 3, 4, 5};

for (int num : numbers) {
    System.out.println("Number: " + num);
}
  1. Branching and Decision-Making

Conditional Statements

Conditional statements are used to execute the program in a certain condition.

If-else statement: when the condition in if the block is true then the code in a curly braces block will execute. if the condition is false then the code in the else block will execute.

Syntax:

if (condition) {
    // Code to be executed if the condition is true
} else {
    // Code to be executed if the condition is false
}

Example:

int number = 10;

if (number % 2 == 0) {
    System.out.println("The number is even.");
} else {
    System.out.println("The number is odd.");
}

Nested if-else statement: Nested if statements are if statements placed inside other if or else blocks. They allow for more complex decision-making by checking multiple conditions.

Example:

int age = 25;
boolean isStudent = false;

if (age > 18) {
    if (isStudent) {
        System.out.println("You are an adult student.");
    } else {
        System.out.println("You are an adult.");
    }
} else {
    System.out.println("You are a minor.");
}

Ternary Operator

It is a simpler way to write if-else statement.

Syntax:

result = (condition) ? value1 : value2;

Example:

int x = 5;
int y = (x > 0) ? 10 : -10;//the value of y is set to 10 
//if x is greater than 0, and -10 otherwise.
System.out.println("Value of y: " + y);
  1. Jump Statements

Jump statements change the flow of the program.

Break Statement

The break statement is used to exit a loop or switch statement before it naturally reaches its end or completion.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i is 5
    }
    System.out.println("i: " + i);
}

Continue Statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration and move to the next iteration.

Example:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    System.out.println("Odd number: " + i);
}

Return Statement

The return statement is used to exit a method and optionally return a value.

Example:

public int add(int a, int b) {
    int sum = a + b;
    return sum;
}

Thank You😊