Decision Statements

Using decision statements, we can make our programs execute certain statements depending on some conditions. Next we illustrate the decision statements supported by C++.

‘ if ‘ statement
It consist of a boolean expression and we execute the set of statements in the ‘ if ‘ block depending on the value (True/False) of the boolean expression. Following program demonstrates the use of ‘ if ‘ statement :

/*
 * This program takes an integer and checks if it is less than 100
 */

#include<iostream>
using namespace std;

int main() {
   int num = 87;
   // check the condition
   if ( num < 100 ) { // if block starts here
      // condition is true, so print the following
      cout << "No. is less than 100 " << endl;
   } // if block ends here
   return 0;
}

‘ if … else ‘ statement
This statement is used when there are multiple conditions depending on which different statements are to be executed. See the following program to understand the use of ‘ if … else ‘ construct :

/*
 * This program finds the larger of two integers
 */

#include<iostream>
using namespace std;

int main() {
   int x = 5, y = 7;
   // test a condition (x > y) to take decision 
   if ( x > y ) { // if block starts
      // condition is true, so print the following
      cout << "The greater number is " << x << endl;
   } // if block ends
   else { // else block starts
      cout << "The greater number is " << y << endl;
   } // else block ends
   return 0;
}

Following program shows a chain of if … else statements :

/* 
 * This program assigns grade to students depending on marks obtained
 * Marks Obtained     Grade
 *     >= 90            A
 * >= 80 and < 90       B
 * >= 70 and < 80       C
 * >= 60 and < 70       D
 * >= 50 and < 60       E
 *     < 50             F
 */

#include<iostream>
using namespace std;

int main() {
   int marks = 63;
   char grade;
   // Assign the grade depending on marks
   if ( marks >= 90 ) {
      grade = 'A';
   }
   else if ( marks >= 80 && marks < 90 ) {
      grade = 'B';
   }
   else if ( marks >= 70 && marks < 80 ) {
      grade = 'C';
   }
   else if ( marks >= 60 && marks < 70 ) {
      grade = 'D';
   }
   else if ( marks >= 50 && marks < 60 ) {
      grade = 'E';
   }
   else if ( marks < 50 ) {
      grade = 'F';
   }
   cout << "Your Grade is " << grade << endl;
   return 0;
}

‘ switch ‘ statement
switch statement is used when a condition or a variable has to be tested for equality against multiple values. Following program illustrates the use of switch construct :

/* 
 * This program gives comments to students depending on their grades
 * Grade   Comments
 *   A     Outstanding
 *   B     Excellent 
 *   C     Very Good
 *   D     Average
 *   E     Below Average
 *   F     Fail
 */

#include<iostream>
using namespace std;

int main() {
   char grade = 'B';
   // Print comment depending on grade
   switch (grade) { // switch block starts here 
      case 'A' : cout << "Outstanding" <<endl;
            	 break; // break out of switch block 
      case 'B' : cout << "Excellent" <<endl;
            	 break;
      case 'C' : cout << "Very Good" <<endl;
            	 break;
      case 'D' : cout << "Average" <<endl;
            	 break;
      case 'E' : cout << "Below Average" <<endl;
            	 break;
      case 'F' : cout << "Fail" <<endl;
             	 break;
      default  : cout << "Invalid Grade" <<endl;
            	 break;
   }
   return 0;
}

Nesting of Decision statements
We can use a decision statement inside the block of another decision statement. This is known as nesting. See the following program to understand nesting :

/*
 * This program calculates the bonus for an employee depending
 * on the following conditions :-
 * 1) Employee should be regular
 * 2) Bonus = (No. of years served * 1 month salary) if years served <= 3
 * 3) Bonus = 3 months salary if years served > 3
 */

#include<iostream>
#include<cctype>
using namespace std;

int main() {
   char emp_type; // Employee Type
   int years_served;
   float salary, bonus;
   cout << "Enter Employee Type (R/r for regular) : ";
   cin >> emp_type;
   emp_type = toupper(emp_type);
   if ( emp_type == 'R' ) { // regular employee
      cout << "Enter no. of years served : ";
      cin >> years_served;
      cout << "Enter salary : ";
      cin >> salary;
      if ( years_served <= 3 ) {
         bonus = salary * years_served;
      }
      else {
         bonus = salary * 3;
      }
      cout << "Bonus : " << bonus << endl;
   }
   else { // Non-regular employee 
      cout << "Not eligible for bonus" << endl ;
   }
   return 0;
}

 

About    Contact    Sitemap    Terms    Privacy