Page 277 - Computer_Science_F5
P. 277

Computer Science  The following C++ Program Example 4.23 enables users to know the type of the
           game through the number of players using switch case statement. The output of the
           program is shown in Figure 4.45.

                    Program Example 4.23:
          FOR ONLINE READING ONLY
             Identifying the type of games using a switch case statement

                     #include<iostream>
                     using namespace std;
                     int main()
                     {
                           int n;
                           cout<<”Enter number of players to know type of the game: “;
                           cin>>n;
                     switch(n)
                     {
                           case 5:
                           cout<<”Basketball”<<endl;
                           break;
                           case 6:
                           cout<<”Volleyball”<<endl;
                           break;
                           case 9:
                           cout<<”Baseball”<<endl;
                           break;
                           case 11:
                           cout<<”Football”<<endl;
                           break;
                           default:
                           cout<<”Select 5, 6, 9 and 11 only”<<endl;
                     }
                     return 0;
                     }

             Output:








                                     Figure 4.45: Output of the program



                Exercise 4.12

             1.  What is the function of the “break” keyword in a switch-case statement?
             2.  Describe the advantages of using a switch-case statement over if statement?
             3.  Which data types are required for the switch variables?


                                                 268
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   268                                                     23/07/2024   12:34
   272   273   274   275   276   277   278   279   280   281   282