Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 153 - CoreGPT(Podar)_Flipbook_C8
P. 153

    The if…else…if Ladder

                     The if…else…if ladder is a conditional control                      True
                                                                            Expression 1        Statement 1
                     structure used to test multiple conditions in a
                     top-down  approach.  The  program  evaluates
                                                                                  False
                     each  condition  one  by  one.  As  soon  as  a
                     condition is true, the corresponding statement                      True   Statement 2
                                                                            Expression 2
                     executes  and  the  rest  of  the  conditions  are
                     ignored. If none of the conditions are true, the              False
                     final  else  statement  executes.  The  syntax  of
                                                                                         True
                     if...else…if ladder is shown below:                    Expression 3        Statement 3
                     if(expression1)

                     {                                                               False      Body of else
                         statement1;
                                                                                                          Statement just
                     }                                                                                 below if...else...if ladder
                     else if(expression2)
                     {
                         statement2;

                     }
                     else if(expression3)
                     {
                         statement3;
                     }
                     // More else if statements can be added if needed
                     else
                     {

                         statementN;
                     }
                     Program  5:  To  check  whether  a  given  number  is  two-digits,  three-digits,  four-digits  or
                     five-digits number.

                     #include<iostream.h>
                     #include<conio.h>
                     void main()
                     {
                         clrscr();
                         long int num;
                         cout<< "Enter a number: ";
                         cin>> num;
                         if(num >= 10 && num <= 99)
                         {

                                                                                      Developing Programs in C++        151

| 148 149 150 151 152 153 154 155 156 157 158 |