If/else Structure
We have seen that the if structure executes its block of statement(s) only when the condition is true, otherwise the statements are skipped. The if/else structure allows the programmer to specify that a different block of statement(s) is to be executed when the condition is false. The structure of if/else selection is as follows.
/* This program checks the age of Amer and Amara’s and
displays the appropriate the message. The program is using
two if statements.*/
# include <iostream.h>
main ( )
{
int AmerAge, AmaraAge;
//prompt the user to enter Amer’s
age
cout << “Please enter Amer’s age “ ;
cin >> AmerAge;
//prompt the user to enter Amara’s
age
cout << “Please enter
Amara’s age “ ;
cin >> AmaraAge;
//perform the test
if (AmerAge > AmaraAge )
{
cout << “ Amer is older than
Amara”;
}
if (AmerAge < AmaraAge )
{
cout << “ Amer is younger than Amara”;
}
}
Else
#include<iostream>
using namespace std;
main()
if (AmerAge > AmaraAge )
{
cout << " Amer is older than Amara";
}
else
{
cout << " Amer is younger than Amara";
}
0 Comments