Decision Making & Looping Structure in C++

Decision Making Structure

  • Conditional statement or decision making statement allows to make a decision, based on a condition.
  • It specifies one or more conditions to be tested or evaluated by the programmer.
Following are the types of decision making statements:

1. If Statement
2. If . . . Else Statement
3. Nested If Statements
4. Switch Statement

1. If Statement

  • If statement is the simplest way to modify the control flow of program.
  • It is a conditional branching statement.
  • This statement consists of a boolean expression followed by one or more statements.
Syntax:
if (condition)
{
     Statement 1;
     Statement 2;
     .
     .
     .
}

Example : Demonstrating the If Statement

#include <iostream>
using namespace std;
int main()
{
     int num1=10, num2=20;
     if(num1 < num2)
     {
          cout<<"Num2 is greater";
     }
     return 0;
}


Output:
Num2 is greater

2. If . . . Else Statement

  • It is a two-way decision making statement.
  • When the boolean expression becomes false then else block will be executed.
  • It is used to make decisions and execute statements conditionally.
Syntax:
if(Condition)
{
     Statements;
}
else
{
     Statements;
}

Flow Diagram of If-Else Statement
if else diagram

Example : Demonstrating the If-Else Statement

#include <iostream>
using namespace std;
int main()
{
     int num1=10, num2=20;
     if(num1 > num2)
     {
          cout<<"Num2 is greater";
     }
     else
          cout<<"Num1 is smaller";
     return 0;
}


Output:
Num1 is smaller

3. Nested If . . . Else Statements

  • Nested If . . . Else statements allow the use of one if or else if statement inside another if or else statements.
  • This statement is like performing another if condition for true or false boolean value.
Syntax:
if (Condition)
{
     Statements;
}
else if (Condition n)
{
     Statements;
}
else
{
     Statements;
}

Example : Program demonstrating Nested If-Else Statements

#include <iostream>
using namespace std;
int main()
{
     int num1=20, num2=10;
     if(num1 > num2)
     {
          cout<<"Num2 is greater";
     }
     else if (num1 < num2)
     {
          cout<<"Num1 is smaller";
     }
     else
     {
          cout<<"Num1 and Num2 are equal";
     }
     return 0;
}


Output:
Num2 is greater

4. Switch Statement

  • Switch statement is used to perform different actions on different conditions.
  • This statement compares the same expression to several different values.
Following are the rules for Switch statement:

1. Switch case should have at most one default label.
2. Default case is optional.
3. Case labels must be unique, end with colon, integral type and have constant expression.
4. Break statement takes control out of the switch and two or more cases may share one break statement.
5. Relational operators are not allowed in Switch statement.
6. Macro identifier and Const variable are allowed in switch case statement.
7. Empty switch case is allowed.
8. Nesting switch is allowed.
9. Default case can be placed anywhere in the Switch statement.

Syntax

switch (Expression)
{
     case condition1:
          //Statements;
          break;
     case condition2:
          //Statements;
          break;
     case condition3:
          //Statements;
          break;
          .
          .
     case condition n;
          //Statements;
          break;
     default:
          //Statement;
}


Flow Diagram of Switch Statement
switch flow diagram

Example : Demonstrating execution of Switch statement

#include <iostream>
using namespace std;
int main()
{
     char color='O';
     switch(color)
     {
           case 'R': cout<<"Red"<<endl;
               break;
           case 'G': cout<<"Green"<<endl;
               break;
           case 'B': cout<<"Blue"<<endl;
               break;
           case 'O': cout<<"Orange"<<endl;
               break;
           case 'P': cout<<"Pink"<<endl;
               break;
           case 'W': cout<<"White"<<endl;
               break;
           case 'Y' : cout<<"Yellow"<<endl;
               break;
           default: cout<<"Inavlid Color"<<endl;
     }
     return 0;
}


Output:
Orange