If statement in Java

If statement allows us to run a part of the program depending on some condition. If the condition is true a block of code is executed and if it is false another block of code is executed.

If-statement takes following four forms:

  1. if
  2. if else
  3. if-else-if Ladder
  4. Nested ifs

We will look at each one of these one by one.


If statement

The control flow diagram for if-statement is shown below:

 

Control flow in if statement
if statement

 

The general form of if statement is:

If the condition evaluates to true, statement1 is executed otherwise the code skips the statement1 and whatever follows is executed.

For example:

The output is:

 


if else statement

The control flow diagram for if else statement is shown below:

 

Control flow in if else statement
if else statement

 

The general form of if else statement is:

If the condition evaluates to true, statement1 is executed otherwise statement2 is executed. In any case only one of the two statements is executed and after that the control flows to the code immediately after statement2.

For example:

The output is:

Note that:

  • Each of statement1 and statement2 may be a single statement or a compound statement. In case it is a compound statement a block of code in curly braces is used.
  • condition is any expression that evaluates to a boolean value. You can even use a boolean variable instead.
  • else part is optional. In that case it becomes if-statement described above.

    The output is:

 


if-else-if ladder

The control flow diagram for if-else-if ladder is shown below:

 

Control flow in if else if ladder
if else if ladder

 

The general form of if-else-if ladder is:

It allows us to choose one among multiple options.

  • As soon as the first if clause is encountered, condition1 is tested, if it evaluates to truestatement1 is executed, other conditions are not tested and control flows to the code after the last statement of the if-else-if ladder. If it evaluates to false condition2 is tested and so on.

In any case only one of the statements is executed.

  • If none of the conditions evaluate to true then the final else statement is executed. It acts as a default statement to execute.

However, it is optional to include the last else statement, in that case there is no default statement to execute and if none of the conditions evaluate to true, nothing happens and the control flows to the statment following the if-else-if ladder.

The output is:

 


Nested ifs

The control flow diagram for nested if statement is shown below:

 

Control flow in nested if statement
Nested if statement

 

The general form of nested if-statement is:

If any of the statements in an if-else ladder is replaced by a block of code which itself contains some if-statements then the ifs are said to be nested.

Simply put, if an if-else statement is contained inside another if-else statement, then ifs are said to be nested.

Statement2 is run only when both condition1 and condition2 are true. Statement3 is run when condition1 is true but condition3 is false.

The output is:

In such cases a doubt always arises; which else correspond to which if.  Remember, the else statement always refers to the nearest if in the same bolck which is not associated with an else.

In the example above, else on line 12 corresponds to if on line 9(nearst if), else on line 16 corresponds to if on line 5 and not 6. Although if on line 6 is nearst unassociated if to else on line 16, it is not in the same block.