C++ Programming

C++ Programming

Nested If Statements


Introduction

A nested if is an if that has that has another if in its body or in its else body. The nested ifs can have one of the following three forms:

Form I

if( condition1 )
{
  if ( condition2 )
   statement1;
  else
   statement2;
}
else
{
//body of else loop
}

Form II

if( condition1 )
{
//body of the if loop
}
else
{
  if ( condition2 )
statement1;
else
statement2;

}

Form III

if( condition1 )
{
  if ( condition2 )
statement1;
else
statement2;

}
else
{
  if ( condition3 )
   statement3;
  else
   statement4;

}

The if-else-if Ladder

A common programming construct in C++ is the if-else-if ladder, which is often also called the if-else-if staircase because of its appearance. It takes the following form:

if( condition1 )
{
//body of if loop
}
else
{
  if ( condition2 )
statement1;
else
{
    if ( condition3 )
     statement2;
    else
     statement3;
  }
}

The expressions are evaluated from the top downward. As soon as condition1 evaluates to true, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expressions are true, the enormous else gets executed. If the enormous else is missing, no action takes place if all the other conditions are false. 

Although it is technically correct to have an indentation in the if-else-if ladder as shown above, however it generally leads to overly deep indentation. So to simplify it, the if-else-if ladder is generally indented like this:

if( condition1 )
{
//body of if loop
}
else if( condition2 )

statement1;
else if( condition3 )
statement2;
else
statement3;

Now compare this indentation to the one above. You’ll note that this is easier to understand. But you may ask what is the purpose of the else-if statement. The else-if statement is used when we need to check multiple statements.

The nested if-else statement introduces a source of potential ambiguity referred to as a dangling-else problem. This problem arises when in a nested if-else statement, the number of ifs is more than the number of else clause. The question then arises, with which if does the additional else clause property matchup. For instance,

if( condition1 )
   if( condition2 )
     statement1;
else
   statement2;

The indentation in the above code fragment indicates that the programmer wants the else to be with the outer if. However, C++ matches an else with the preceding unmatched if. In this case, the actual evaluation of the if-else statement will be shown below:

if( condition1 )
if( condition2 )

     statement1;
   else
     statement2;

If the inner expression is false, then the else clause gets executed.

The last else statement goes with the immediately preceding if statement that does not already have an else statement. Here, statement2 is executed if condition2 evaluates to false.

The Weekly Program

Now let’s create a program to check whether a person is eligible to vote.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int age;
char name[20],cty[30];
cout<<"Enter your Name: ";
cin>>name;
cout<<"Enter your Age: ";
cin>>age;
cout<<"Enter your Native Country: ";
cin>>cty;
cout<<endl;
if(age>=18)
cout<<name<<" from "<<cty<<" is eligible to vote";
else
cout<<name<<" from "<<cty<<" is not eligible to vote";
getch();
}

According to the program, if your 18 and above you’re eligible to vote. And if you’re below 18, then your not eligible to vote.

Execution of the Program

Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!