Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 148 - CoreGPT(Podar)_Flipbook_C8
P. 148

    where,

                          Conditional_expression is an expression that evaluates to true or false.
                          If conditional_expression is true, then expression1 is executed.
                          If conditional_expression is false, then expression2 is executed.

                      Program 8: To check the greatest in two numbers.
                      #include<iostream.h>

                      #include<conio.h>
                      void main()
                      {
                          clrscr();
                          int a, b;
                                                                                            a is greater than b
                          a = 20;

                          b = 5;
                           (a > b) ? cout<< "a is greater than b" : cout<< "b is greater than
                            a";
                          getch();
                      }

                            SELECTION STRUCTURES IN C++

                      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

                                                                                                                    False
                      The if statement is the simplest conditional statement. It selects and         Test Expression
                      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";

                    146     Premium Edition-VIII

| 143 144 145 146 147 148 149 150 151 152 153 |