Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 151 - CoreGPT(Podar)_Flipbook_C8
P. 151

    if(marks > 200)

                         {                                                  Enter your total marks: 250
                             cout<< "Pass";                                 Pass
                         }
                         else
                                                                            Enter your total marks: 200
                     {
                                                                            Fail
                             cout<< "Fail";
                         }
                             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;
                                                                                Enter the value of a: 21
                         a = a + 1;
                                                                                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)
                     {

                                                                                      Developing Programs in C++        149

| 146 147 148 149 150 151 152 153 154 155 156 |