Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 150 - CoreGPT(Podar)_Flipbook_C8
P. 150

    if(n > 20)

                          {                                                   Enter a number less than 21: 25
                           cout<< "Too Large Number";                         Too Large Number_
                          }
                          getch();
                      }
                      The if…else Statement

                      The if…else statement is used in programming to make decisions. If the condition is True, the
                      program runs the if block. If it is False, the program runs the else block instead. This helps the
                      program choose between two actions based on the condition.
                      The syntax of the if…else statement is given below:

                      if(condition)
                                                                                         Test Expression
                      {
                                                                                                                    False
                      Statement 1;
                                                                                               True
                      }
                                                                                           Body of if         Body of else
                      else
                      {
                      Statement 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()

                      {
                          clrscr();
                          int marks;
                          cout<< "Enter your total marks: ";
                          cin>> marks;

                    148     Premium Edition-VIII

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