Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Core Java - Control Statements (if, else, switch, loops)

6. Control Statements (if, else, switch, loops)

Control statements in Java allow you to control the flow of your program, make decisions, and repeat tasks. In this section of the Core Java tutorial, we'll explore control statements, including 'if' , 'else', 'switch', and various types of loops, with detailed explanations and examples.

'if' Statement:

The 'if' statement is used to execute a block of code only if a specified condition is 'true'. It's one of the most fundamental control structures in Java.

Syntax:
if (condition) {
    // Code to execute if the condition is true
}

 

Example:
int age = 18;
if (age >= 18) {
    System.out.println("You are an adult.");
}

 

In this example, the code inside the 'if' block is executed because the condition  'age >= 18' is 'true'.

'if-else'Statement:

The 'if-else' statement is used to execute one block of code if a condition is 'true' and another block if the condition is 'false'.

Syntax:
if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

 

Example:
int age = 15;
if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are not yet an adult.");
}

 

In this example, the second 'System.out.println' statement is executed because the condition 'age >= 18'is 'false'.

'switch' Statement:

The 'switch' statement is used for multiple branches of execution based on the value of an expression. It's an alternative to using multiple 'if-else' statements.

Syntax:
switch (expression) {
    case value1:
        // Code to execute if expression == value1
        break;
    case value2:
        // Code to execute if expression == value2
        break;
    // ...
    default:
        // Code to execute if expression doesn't match any case
}
 
Example:
int dayOfWeek = 3;
switch (dayOfWeek) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    // ...
    default:
        System.out.println("Invalid day");
}

 

In this example, the code inside the  'case 3'block is not executed because 'dayOfWeek'is equal to 3, and it matches the 'case 3' condition.

4. Loops:

Loops in Java allow you to execute a block of code repeatedly. Java provides several types of loops:

1. 'for' Loop:

The  'for' loop is used when you know in advance how many times you want to execute a block of code.

Syntax:
for (initialization; condition; iteration) {
    // Code to execute repeatedly
}
 
Example:
for (int i = 1; i <= 5; i++) {
    System.out.println("Iteration " + i);
}

 

This 'for' loop will execute the code block five times, printing "Iteration 1" through "Iteration 5."

2. 'while'Loop:

The 'while' loop is used when you want to execute a block of code as long as a certain condition is 'true'.

Syntax:
while (condition) {
    // Code to execute repeatedly
}
 
Example:
int count = 0;
while (count < 3) {
    System.out.println("Count: " + count);
    count++;
}

 

This 'while' loop will execute the code block three times, printing "Count: 0," "Count: 1," and "Count: 2."

3. 'do-while' Loop:

The  'do-while' loop is similar to the 'while' loop, but it always executes the code block at least once, even if the condition is initially false.

Syntax:
do {
    // Code to execute repeatedly
} while (condition);
 
Example:
int i = 1;
do {
    System.out.println("Iteration " + i);
    i++;
} while (i <= 3);

 

This 'do-while' loop will execute the code block three times, printing "Iteration 1," "Iteration 2," and "Iteration 3."

These control statements and loops are essential tools for creating flexible and dynamic Java programs. They allow you to make decisions and repeat tasks, enabling your programs to respond to different scenarios and handle large sets of data efficiently. In the next sections of this Core Java tutorial, we'll explore more advanced programming constructs and how to use them effectively in Java applications.