Page 42 - Modular_V1.1_Flipbook
P. 42

The syntax of the if…else statement is given below:

                  if (condition)
                  {
                                                                                     Test Expression
                  Statements 1;
                                                                                                                False
                  }
                                                                                           True
                  else
                  {                                                                    Body of if         Body of else

                  Statements 2;
                  }                                                                  Statement just
                                                                                     below if...else
                  Some examples of valid if-else statement are:
                  •    if (num1 > num2)

                       cout << num1 << “ is greater than “ << num2;
                       else
                       cout << num2 “ is greater than “ << num1;
                  •    if ((gender == ‘M’) || (gender == ‘m’))

                       cout << “You are a male”;
                       else

                       cout << “You are a female”;
                  Program 2: To show the pass/fail status of a student.
                  #include <iostream.h>

                  #include<conio.h>
                  void main()
                  {
                  int marks;
                  cout<<”Enter your total marks: “;

                  cin>>marks;
                  if(marks > 200)
                  {                                                                        Output 1:
                  cout<<”Pass”;                                                            Enter your total marks: 250
                  }                                                                        Pass
                                                                                           Output 2:
                  else
                                                                                           Enter your total marks: 200
                  {
                                                                                           Fail
                  cout<<”Fail”;
                  }
                  }

                  Program 3: To use the ternary operator in place of if…else statement.
                  #include<iostream.h>
                  #include<conio.h>



                  40      Touchpad MODULAR (Version 1.1)-X
   37   38   39   40   41   42   43   44   45   46   47