Page 54 - Modular_V2.0_C++_Flikpbook
P. 54

switch(code)
                      {
                      case 'd':
                          cout<<"The season is dry";
                          break;
                      case 'w':
                          cout<<"The season is winter";
                                                                                 DOSBox 0.74, Cpu speed: max 100% cycles,
                          break;
                                                                              Enter a character r
                      case 'r':
                                                                              The season is rainy_
                          cout<<"The season is rainy";
                                                                                 DOSBox 0.74, Cpu speed: max 100% cycles,
                          break;
                                                                              Enter a character x
                      default:
                                                                              Word code
                          cout<<"Wrong code";
                      }

                      getch();
                  }


                      Recap



                          Conditional statements are used to test conditions and decide the flow of a program on the
                          basis of result of the conditions.
                         The if statement selects and executes the statement(s) based on a given condition.
                         The if…else statement executes one block if the condition is True, otherwise, it runs the else
                          block.
                         A nested if statement in C++ is an if statement inside another if, where the inner if runs only if
                          the outer if is true.
                         The if…else…if ladder checks conditions in order, executing the first true one or the else block
                          if none are true.
                         The if…else…if ladder checks conditions in order, executing the first true one or the else block
                          if none are true.



                                                         Exercise



                  A.   Tick ( ) the correct option.
                       1.  What will be the output of the following code?

                           if(True)
                           cout<<"Hello";
                           else
                           cout<<"Hi";
                           a.  Hello                                    b.  Hi
                           c.  Hello Hi                                 d.  Nothing will print


                   52
                          Touchpad MODULAR (Ver. 2.0)
   49   50   51   52   53   54   55   56   57   58   59