C++ Decision Making

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this C++ tutorial, let us discuss decision making, its types with a sample C++ program to understand the concepts better.

Introduction of C++ Decision making

Decision making is about selecting the order of execution of statements based on certain conditions until certain specified conditions are satisfied. 

Types of decision-making statements in C++

  • if
  • if-else
  • switch
  • nested-if
  • nested-switch

if

An if statement contains a boolean expression followed by one or more statements.

Syntax

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true
}

Program for if statements

#include <iostream>
using namespace std; 
int main () {
   // local variable declaration:
   int a = 10; 
   // check the boolean condition
   if( a < 20 ) {
      // if condition is true then print the following
      cout << "a is less than 20;" << endl;
   }
   cout << "value of a is : " << a << endl; 
   return 0;
}

Output

a is less than 20;
value of a is : 10

if-else

An if statement can be followed by an optional else statement, that executes when the boolean expression is false.

Syntax

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true
} else {
  // statement(s) will execute if the boolean expression is false
}

Program for if-else statements

#include <iostream>
using namespace std; 
int main () {
   // local variable declaration:
   int a = 100; 
   // check the boolean condition
   if( a < 20 ) {
      // if condition is true then print the following
      cout << "a is less than 20;" << endl;
   } else {
      // if condition is false then print the following
      cout << "a is not less than 20;" << endl;
   }
   cout << "value of a is : " << a << endl; 
   return 0;
}

Output

a is not less than 20;
value of a is : 100

Switch

A switch statement allows a variable to be examined for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax

switch(expression) {
   case constant-expression :
      statement(s);
      break; //optional
   case constant-expression :
      statement(s);
      break; //optional  
   // you can have any number of case statements.
   default : //Optional
      statement(s);
}

Program for Switch statements

#include <iostream>
using namespace std; 
int main () {
  // local variable declaration:
  char grade = 'D';
  switch(grade) {
     case 'A' :
        cout << "Excellent!" << endl; 
        break;
     case 'B' :
     case 'C' :
        cout << "Well done" << endl;
        break;
     case 'D' :
        cout << "You passed" << endl;
        break;
     case 'F' :
        cout << "Better try again" << endl;
        break;
     default :
        cout << "Invalid grade" << endl;
  }
  cout << "Your grade is " << grade << endl; 
  return 0;
}

Output

You passed
Your grade is D

nested-if

It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax

if( boolean_expression 1) {
   // Executes when the boolean expression 1 is true
   if(boolean_expression 2) {
      // Executes when the boolean expression 2 is true
   }
}

Program for nested-if statements

#include <iostream>
using namespace std; 
int main () {
   // local variable declaration:
   int a = 100;
   int b = 200; 
   // check the boolean condition
   if( a == 100 ) {
      // if condition is true then check the following
      if( b == 200 ) {
         // if condition is true then print the following
         cout << "Value of a is 100 and b is 200" << endl;
      }
   }
   cout << "Exact value of a is : " << a << endl;
   cout << "Exact value of b is : " << b << endl;
    return 0;
}

Output

Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

nested-switch

It is probable to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will occur.

Syntax

switch(ch1) {
   case 'A': 
      cout << "This A is part of outer switch";
      switch(ch2) {
         case 'A':
            cout << "This A is part of inner switch";
            break;
         case 'B': // ...
      }
      break;
  case 'B': // ...
}

Program for nested-switch cases

#include <iostream>
using namespace std; 
int main () {
   // local variable declaration:
   int a = 100;
   int b = 200; 
   switch(a) {
      case 100: 
         cout << "This is part of outer switch" << endl;
         switch(b) {
            case 200:
               cout << "This is part of inner switch" << endl;
         }
   }
   cout << "Exact value of a is : " << a << endl;
   cout << "Exact value of b is : " << b << endl;
   return 0;
}

Output

This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200

Leave a Comment

Your email address will not be published. Required fields are marked *