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

5                                                                    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, the statements inside the if               True
                  block will be executed. Otherwise, the control of execution is passed to
                                                                                                   Body of if
                  the next statement after the if block. The syntax of the if statement is
                  given below:
                                                                                                 Statement just
                       if(condition)
                                                                                                    below if
                       {
                           statement(s);
                       }

                  If the value of the condition is nonzero (True), the body of the if statement gets executed. Some
                  examples of valid if statement are:
                  •  if(x > 0)

                      cout<< x << " is a positive number";
                  •  if(x) // Same as: if (x != 0)
                      cout<< x << " is a nonzero number";

                  •  if(guess == num)
                      cout<< "You guessed the right number!";
                   42
                          Touchpad MODULAR (Ver. 2.0)
   39   40   41   42   43   44   45   46   47   48   49