Types of control flow statements in java with examples

Alan 133 Published: 09/07/2024

Types of control flow statements in java with examples

I can respond in English only for you!

Control Flow Statements in Java

In Java, control flow statements are used to control the order and flow of execution of your program's code. They help determine what code should be executed next or whether certain parts of your code should be skipped.

1. If-Else Statement

The if-else statement is one of the most commonly used control flow statements in Java. It allows you to execute a block of code if a specified condition is true and an alternative block of code if the condition is false.

Example:

int x = 10;

if (x > 5) {

System.out.println("x is greater than 5");

} else {

System.out.println("x is less than or equal to 5");

}

In this example, if x is greater than 5, the code inside the if block will be executed. Otherwise, the code inside the else block will be executed.

2. Switch Statement

The switch statement allows you to execute a specific block of code based on the value of an expression or variable.

Example:

int day = 3;

switch (day) {

case 1:

System.out.println("It's Monday");

break;

case 2:

System.out.println("It's Tuesday");

break;

default:

System.out.println("Invalid day of the week");

}

In this example, based on the value of day, different messages will be printed to the console.

3. Loops (For and While)

Java provides two types of loops: for loops and while loops.

a. For Loop

The for loop allows you to execute a block of code repeatedly based on a set of initialization, condition, and increment/decrement statements.

Example:

for (int i = 0; i < 5; i++) {

System.out.println("Loop iteration: " + i);

}

In this example, the code inside the for loop will be executed five times with i ranging from 0 to 4.

b. While Loop

The while loop allows you to execute a block of code repeatedly as long as a specified condition is true.

Example:

int i = 0;

while (i < 5) {

System.out.println("Loop iteration: " + i);

i++;

}

In this example, the code inside the while loop will be executed five times with i ranging from 0 to 4.

4. Do-While Loop

The do-while loop is similar to a while loop, but it executes the code block first and then checks the condition.

Example:

int i = 0;

do {

System.out.println("Loop iteration: " + i);

i++;

} while (i < 5);

In this example, the code inside the do-while loop will be executed five times with i ranging from 0 to 4.

5. Break and Continue Statements

The break statement is used to exit a loop or switch statement, while the continue statement skips the current iteration of a loop and moves on to the next one.

Example:

for (int i = 0; i < 5; i++) {

if (i == 2) {

continue;

}

System.out.println("Loop iteration: " + i);

}

In this example, when i is equal to 2, the loop will skip that iteration and move on to the next one.

These are some of the most common control flow statements in Java. Understanding how to use them effectively can help you write more efficient and readable code.

What are control flow statements in Java?

Control Flow Statements!

In Java, control flow statements are used to control the execution of a program's flow by making decisions, looping through blocks of code, or jumping to specific locations. These statements play a crucial role in writing efficient and effective programs.

1. If-Else Statement

The if-else statement is one of the most common control flow statements in Java. It checks a condition and executes a block of code if it's true, and another block of code if it's false. The basic syntax is:

if (condition) {

// execute if condition is true

} else {

// execute if condition is false

}

2. Switch Statement

The switch statement allows you to perform different actions based on the value of an expression. It evaluates the value and executes the corresponding block of code. The syntax is:

switch (expression) {

case value1:

// execute for value1

break;

case value2:

// execute for value2

break;

default:

// execute if none of the above cases match

}

3. For Loop

The for loop is used to iterate through a block of code repeatedly based on a specific condition. It's commonly used when you need to repeat an action over and over until a condition is met. The syntax is:

for (initialization; condition; increment) {

// execute repeatedly

}

4. While Loop

The while loop is similar to the for loop, but it only checks the condition after each iteration. This means you need to ensure that your loop has a finite number of iterations or have a way to exit the loop. The syntax is:

while (condition) {

// execute repeatedly until condition is false

}

5. Do-While Loop

The do-while loop combines the benefits of while loops and for loops. It executes a block of code at least once, and then continues to repeat if the condition is true. The syntax is:

do {

// execute repeatedly

} while (condition);

6. Break Statement

The break statement is used to exit a loop or switch statement prematurely. This can be useful when you need to stop iterating through code under certain conditions.

7. Continue Statement

The continue statement skips the rest of the current iteration and goes back to the beginning, re-evaluating the condition before continuing the loop.

8. Return Statement

The return statement is used in methods to exit and send a value back to the calling method.

In conclusion, control flow statements are essential for writing robust and efficient Java programs. By using these statements effectively, you can make your code more readable, maintainable, and scalable.

So, which control flow statement will you use next?