Nested if Statement in C++ Programming


A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. when you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.

Syntax:
if(Expression 1)
{
    // Executes when the Expression 1 is true
     if(Expression 2)
     {
        // Executes when the Expression 2 is true
     }
}

The program is written in C++ language and it determines the eligibility of a person to vote based on their age and gender. Here's how the program works:

  • It first declares two variables 'age' and 'gender' to store the input values of age and gender, respectively.
  • The user is then prompted to enter their age, which is read by the 'cin' statement.
  • The program checks if the entered age is greater than or equal to 18. If the condition is true, the program prompts the user to enter their gender.
  • If the user enters 'M' or 'm' as gender, the program displays "Go to Room-5", indicating that the person is eligible to vote and should go to room number 5.
  • If the user enters 'F' or 'f' as gender, the program displays "Go to Room-6", indicating that the person is eligible to vote and should go to room number 6.
  • If the user enters any other character as gender, the program displays "Invalid Gender Input".
  • If the age entered is less than 18, the program displays "Your Age is Under 18 You are Not Eligible For Vote...".
  • Finally, the program returns 0, indicating successful execution.

Source Code

#include<iostream>
using namespace std;
/*
age>=18:
    Male:
        Room-5
    Female:
        Room-6
Not Eligible
*/
int main()
{
    char gender;
    int age;
    cout<<"\nEnter Your Age    : ";
    cin>>age;
    if(age>=18)
    {
        cout<<"\nEnter Your Gender : ";
        cin>>gender;
        if(gender=='M' || gender=='m')
        {
             cout<<"\nGo To Room-5";
        }
        else  if(gender=='F' || gender=='f')
        {
             cout<<"\nGo To Room-6";
        }
        else
        {
            cout<<"\n Invalid Gender Input";
        }
    }
    else
    {
        cout<<"\nYour Age is Under 18 You are Not Eligible For Vote...";
    }
    return 0;
}
 
 

Output

Enter Your Age    : 23

Enter Your Gender : Female

Go To Room-6
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples