Control Statements in Java Programming Language

if, else, if-else, nested if else and switch statements in java

Control statements are used to control the flow of execution in a Java program. There are several types of control statements in Java including if, else if, nested if, and switch statements. In this explanation, we will go over each of these control statements and provide code examples with output.

Control Statements

If Statement

The if statement is used to execute a block of code only if a certain condition is true.

The syntax of the if statement is as follows:

if (condition) {
   // code to be executed if condition is true
}

Here’s an example of an if statement in Java:

int x = 5;

if (x > 0) {
    System.out.println("x is positive");
}

Output:

x is positive

Else If Statement in Java

The else if statement is used to execute a block of code if a certain condition is true and another condition is false. The syntax of the else if statement is as follows:

if (condition1) {
   // code to be executed if condition1 is true
} else if (condition2) {
   // code to be executed if condition2 is true
}

Here’s an example of an else-if statement in Java:

int x = 0;

if (x > 0) {
    System.out.println("x is positive");
} else if (x < 0) {
    System.out.println("x is negative");
} else {
    System.out.println("x is zero");
}

Output:

x is zero

Nested If Statement

A nested if statement is an if statement that is inside another if statement. This allows for more complex conditions to be checked. The syntax of a nested if statement is as follows:

if (condition1) {
   // code to be executed if condition1 is true
   
   if (condition2) {
      // code to be executed if condition1 and condition2 are true
   }
}

Here’s an example of a nested if statement in Java:

int x = 5;
int y = 10;

if (x > 0) {
    if (y > 0) {
        System.out.println("x and y are positive");
    }
}

Output:

x and y are positive

Switch Statement in Java

The switch statement is used to execute different actions based on different conditions. It is often used as a substitute for long if-else chains. The syntax of the switch statement is as follows:

switch (expression) {
   case value1:
      // code to be executed if expression is equal to value1
      break;
   case value2:
      // code to be executed if expression is equal to value2
      break;
   ...
   default:
      // code to be executed if expression is not equal to any of the values
}

Here’s an example of a switch statement in Java:

int day = 4;
String dayString;

switch (day) {
    case 1:
        dayString = "Monday";
        break;
    case 2:
        dayString = "Tuesday";
        break;
    case 3:
        dayString = "Wednesday";
        break;
    case 4:
        dayString = "Thursday";
        break;
    case 5:
        dayString = "Friday";
        break;
    case 6:
        dayString = "Saturday";
        break;
    case 7:
        dayString = "Sunday";
        break;
    default:
        dayString = "Invalid day";
        break;
}

System.out.println(dayString);

Output:

Thursday

Jumping Statements in Java

Continue Statement in Java

The continue statement is used inside loops to skip the current iteration and continue with the next iteration of the loop. The syntax of the continue statement is as follows:

continue;

Here’s an example of the continue statement in Java:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        continue;
    }
    System.out.print(i + " ");
}

Output:

0 1 2 3 4 6 7 8 9

In the example above, the continue statement is used inside a for loop to skip the iteration when the value of i is 5. The output shows that the value 5 is not printed, and the loop continues with the next iteration.

Break Statement in Java Programming

The break statement is used inside loops and switch statements to exit the loop or switch statements early. The syntax of the break statement is as follows:

break;

Here’s an example of the break statement in Java:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.print(i + " ");
}

Output:

0 1 2 3 4

In the example above, the break statement is used inside a for loop to exit the loop when the value of i is 5. The output shows that the loop exits early and only prints values from 0 to 4.

FAQ’s

What is the difference between an if statement and an else if statement?

An if statement is used to execute a block of code only if a certain condition is true. An else-if statement is used to execute a block of code if a certain condition is true and another condition is false. The difference between them is that an if statement is evaluated first, and if its condition is true, the code inside its block is executed. If the condition is false, the else if statement is evaluated, and if its condition is true, the code inside its block is executed. If neither condition is true, the code inside the else block is executed (if there is one).

What is a nested if statement?

A nested if statement is an if statement that is inside another if statement. This allows for more complex conditions to be checked. A nested if statement is evaluated in the same way as a regular if statement, but the code inside its block may contain another if statement.

What is the purpose of a switch statement?

A switch statement is used to execute different actions based on different conditions. It is often used as a substitute for long if-else chains. The switch statement evaluates an expression and compares it to several possible values. If the expression matches one of the values, the code inside the corresponding case block is executed. If there is no match, the code inside the default block is executed.

What is the difference between a continue statement and a break statement?

A continue statement is used inside loops to skip the current iteration and continue with the next iteration of the loop. A break statement is used inside loops and switch statements to exit the loop or switch statement early. The difference between them is that a continue statement skips the current iteration of the loop, but the loop continues with the next iteration. A break statement exits the loop or switch statement entirely and continues with the next statement after the loop or switch statement.