if . . . else Statement

 

Sometimes, we can execute one group of statements if the condition is true and another group of statements if the condition is false, in such a situation, the  if . . . else statement can be used.

 

The if . . . else statement is an extension of simple if statement. The syntax  is :

 

                        if (test condition)

          {

                                    true-block-statement(s) ;

          }

else

          {

                                    false-block-statement(s) ;

          }

          statement-x ;

 

If the test condition is true, then the true-block-statement(s) are executed. If the test condition is false, then the false-block-statement(s) are executed. In either case, either true-block-statement(s) or false-block-statement(s) will be executed, not both.

 

The figure below shows the flowchart of simple if statement :

 

To understand this programming construct, let us consider an example of checking whether the person is eligible to vote or not by getting the age of the person. The task include :

 

  • Getting the input (age) from the user.
  • Making the decision if the age is greater than or equal to 18, then print the person is eligible to vote.
  • Else print the person is not eligible to vote.

 


Nested if . . . else Statement

 

We can write an entire if . . . else construct within either the body of an if statement or the body of an else statement. This is called nesting of ifs. The syntax is :

 

if (test condition 1)

{

if (test condition 2)

                                    {

                                                statement-1 ;

                                    }

                                    else

                                    {

                                                statement-2 ;

                                    }

                        }

else

          {

                                    statement-3 ;

                        }

          statement-x ;

If the test condition 1 is false, the statement-3 will be executed. If the test condition 1 is true, it continues to perform the test condition 2. If the test condition 2 it is true, the statement-1 will be executes. If the test condition 2 is false, the statement-2 will be executed and then the control is transferred to the statement-x.

 

else if Ladder

 

Generally, the case where the statements in the if part of an if . . . else statement is another if statement tends to be confusing and best avoided. However an extremely useful construction occurs when the else part of if statement contains another if . . . else statement. This construction is called as an else if ladder or chain. The syntax is :

 

if (test condition 1)

          {

                                    statement-1 ;

                        }

                        else if (test condition 2)

          {

                                    statement-2 ;

                        }

                        .           .           .

                 .           .           .

          .           .           .

          else if (test condition n)

          {

                                    statement-n ;

                        }

else

          {

default-statement ;

                        }

          statement-x ;

Each condition is evaluated in order and if any condition is true, the corresponding statement is executed and the remainder of the chain is terminated and the control is transferred to the statement-x. The final else statement containing the default statement will be executed if none of the previous n conditions are not satisfied.

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback