Java Control Statements: if, while, switch & for

Control statements in Java are used to control the flow of execution of a program. There are three main types of control statements in Java:

1. if statements:

Used to execute a block of code only if a certain condition is true.

Syntax:

Example:

2. if-else statements:

Used to execute a block of code if the condition is true and another block of code if the condition is false.

Syntax:

Example:

3. Switch statements:

Used to select one of many blocks of code to be executed based on the value of an expression.

Syntax:

Example:

This program will output:

4. while loops:

Used to repeat a block of code while a certain condition is true.

Syntax:

Example:

Output:

5. do-while loops:

Similar to while loops, but the block of code is executed at least once before the condition is checked.

Syntax:

Output:

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

6. for loops:

Used to repeat a block of code a specified number of times. Syntax:

Example:

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top