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

}
                         getch();

                 }
                 Program 3: To use the ternary operator in place of if…else statement.

                 #include<iostream.h>
                 #include<conio.h>
                 void main()

                 {
                     int a, b, c;
                     clrscr();

                     cout<< "Enter the value of a: ";
                     cin>> a;
                     cout<< "Enter the value of b: ";

                     cin>> b;                                                   DOSBox 0.74, Cpu speed: max 100% cycles, Frames
                     a = a + 1;                                             Enter the value of a: 21
                                                                            Enter the value of b: 43
                     b = b - 1;
                                                                            Value of a: 22
                     c = (b > a) ? b : a;                                   Value of b: 41

                     cout<< "\n Value of a: " << a++;                       Value of c: 42
                     cout<< "\n Value of b: " << --b;

                     cout<< "\n Value of c: " << c;
                     getch();
                 }
                 Nested if Statement

                 In C++, you can use an if statement inside another if statement. This is called a nested if statement.

                 The inner if statement runs only if the outer if condition is true. The syntax of nested statement
                 is shown below:

                      if(condition1)
                      {

                          //Statements for condition1
                          if(condition2)

                          {
                               // Statements for condition2 (executed only if condition1
                                  and condition2 are both true)

                          }
                          else

                          {

                                                                                                                  45
                                                                                           Conditional Statements
   42   43   44   45   46   47   48   49   50   51   52