Page 274 - Computer_Science_F5
P. 274
In order to practice on the use of Nested if….to write a program, learn from the
following Program Example 4.22 that demonstrates the use of Nested if statement.
The program enables users to know the type of the game through the number of
players. The output of the program is given in Figure 4.43. Chapter Four: Object oriented programming with C++
FOR ONLINE READING ONLY
Program Example 4.22:
The use of Nested if statement
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<” Choose 5, 6, 9 or 11 players to know type of the game “<<endl;
cout<<”Enter number of players to know type of the game: “;
cin>>n;
if(n==11)
cout<<”Football”<<endl;
else if(n==5)
cout<<”Basketball”<<endl;
else if(n==6)
cout<<”Volleyball”<<endl;
else if(n==9)
cout<<”Baseball”<<endl;
else
cout<<”Wrong choice”<<endl;
return 0;
}
Output:
Figure 4.43: Output of the program
265
Student’s Book Form Five
Computer Science Form 5.indd 265 23/07/2024 12:34

