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:
#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; }
Enter Your Age : 23 Enter Your Gender : Female Go To Room-6To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions