×
☰ See All Chapters

Java Conditional Statements

Statements in Java program are executed sequentially in the order in which they appear. There are situations where we may have to change the order of execution of statements based on certain conditions, or repeat the execution until certain specified conditions are met. This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the interpreter (JVM) to execute certain statements accordingly. Conditional statements are used to change the execution of statements based on conditions. In Java we have the following conditional statements:

  1. if 

  2. if else 

  3. else if 

  4. switch 

if statement

if statement syntax:

if(boolean_condition)

{

/* statement(s) will execute if the boolean condition is true */

}

If the boolean_condition evaluates to true, then statements inside if block will be executed. If the boolean_condition evaluates to false, then statement immediately after if block will be executed. Statement inside if block can be single statement or compound statement enclosed in curly braces (that is, a block). If the statement inside if block is single statement the curly braces are optional.

if else statement

if else statement syntax:

if(boolean_condition)

{

/* statement(s) will execute if the boolean condition is true */

}

else {

/* statement(s) will execute if the boolean condition is false */

}

else block is optional. If the boolean_condition evaluates to true, then the if block will be executed, otherwise else block will be executed.

if-else-if Ladder

if-else-if ladder syntax:

if(boolean_condition 1) {

/* Executes when the boolean condition 1 is true */

}

else if( boolean_condition 2){

/* Executes when the boolean condition 2 is true */

}

else if( boolean_condition 3){

/* Executes when the boolean condition 3 is true */

}

else {

/* executes when the none of the above condition is true */

}

else if and else blocks are optional. If there are number of operations need to be done based on certain conditions then if else ladder is useful. Ladder will be executed from top till any boolean_condition becomes true. If any boolean_condition evaluates to true then that block will be executed. If no boolean_condition evaluates to true then last else block will be executed.

Nested ifs

Nested ifs  syntax:

if( boolean_condition 1)

{

     /* Executes when the boolean condition 1 is true */

        if(boolean_condition 2)

        {

        /* Executes when the boolean condition 2 is true */

        }

}

We can have if, if else, if else ladder inside any if, if else, if else ladder.

switch statement

switch statement syntax:

switch (expression) {

case value_expression:

        statement(s);

        break; /* optional */

case value_expression:

        statement(s);

        break; /* optional */

/* you can have any number of case statements */

default: /* Optional */

        statement(s);

}

switch statement is used to execute a particular set of statements among multiple conditions. It is an alternative to complicated if else if ladder conditions.

  1. When switch block execution starts, this expression will be evaluated to a value. 

  2. The value_expression should be evaluated to a constant value of type same as the expression type in the switch.  

  3. Case block whose value is equal to the switch expression will be executed. 

  4. break is optional. Every case must end with break statement which will help to terminate and transfer the control outside of switch block. If no break is used then execution will continue to next case. 

  5. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.  

Nested switch statements

We can have switch blocks inside other switch block.

Nested switch statements Syntax:

switch (expression) {

case value_expression:

        switch (expression) {

        case value_expression:

                statement(s);

                break;

        case value_expression:

                statement(s);

                break;

        default:

                statement(s);

        }

        break;

case value_expression:

        statement(s);

        break;

default:

        statement(s);

}

 

 


All Chapters
Author