Page 276 - Computer_Science_F5
P. 276

language introduces a switch-case statement to solve this problem. The syntax of the
              Switch case statement is shown as follows:
              switch(variable)
                    {                                                                              Chapter Four: Object oriented programming with C++

                    case 1: statement 1;
          FOR ONLINE READING ONLY
                    break;
                    case 2: statement 2;
                    break;
                    case 3: statement 3;

                    break;
                    default: default statement
                    break;
                    }


              Figure 4.44 shows the execution flowchart of the switch case statement.





                                                   True
                                      case 1               statement 1

                                          False
                                                   True
                                      case 2               statement 2

                                          False

                                                   True
                                       case 3              statement 3


                                          False

                                  default statement





                                 Figure 4.44: A flowchart for a Switch case statement
              Note:
              It is a good practice in programming to write cases in sequential order and end with
              default case, but it is not a must to follow that order. The switch expression should
              be enclosed in brackets.

                                                    267
               Student’s Book  Form Five



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