Page 40 - Modular_V1.1_Flipbook
P. 40

05                                 CONDITIONAL



                                                                                           STATEMENTS













                           Your Aim


                           to learn about:
                                  The if Statement                                 The if…else Statement
                                    The if…else…if Ladder                            The switch Statement
                                     Difference between if and switch Statements            More Solved Programs


                  Conditional statements are also known as decision making statements or selection statements.
                  These statements are used to test the conditions and decide the flow of a program on the basis
                  of result of these conditions. The result of these conditions may be true or false. C++ provides
                  the following conditional statements:

                           if statement       if…else statement       if…else…if ladder         switch statement

                      THE IF STATEMENT
                  The if statement is the simplest conditional statement. It selects and         Test Expression  False
                  executes the statement(s) based on a given condition. If a particular
                  condition or expression evaluates to True, then a set of statements will              True
                  be executed. Otherwise, the control of execution is passed to the next
                  statement after the if block. The syntax of the if statement is given below:     Body of if
                  if(condition){

                  statement(s);                                                                  Statement just
                                                                                                    below if
                  }
                  If  the  value  of  the  condition  is  nonzero,  then  the  body  of  if gets
                  executed. Some examples of valid if statement are:

                  •    if (x > 0)                                              Clickipedia
                       cout << a << “is a positive number”;
                                                                                You  can  also write  conditional
                  •    if (x) // Same as : if (x != 0)
                                                                                statements  without  the  braces  if
                       cout << x << “is a nonzero number”;
                                                                                there is only one line of code.
                  •    if (guess == num)

                       cout << “You guessed the right number”;
                  •    if (x <= y)

                       cout << x << “ is greater than ” << y;

                  38      Touchpad MODULAR (Version 1.1)-X
   35   36   37   38   39   40   41   42   43   44   45