C++ Decision Making

    Some situation arises in programming where we need to make the decision which block of code should execute and which not. For instance, if there are 3 blocks of code and based on the user input we want to execute only one block of code so for that we use Decision Making statements.

    C++ Decision Making

    These decision-making statements decide the flow of program execution and in C++ we have the following decision-making statements:

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

    1. if statements

    if statements are the simplest statements and operate on boolean data types true or false. If the condition inside the if parenthesis is true then only if block executes if the condition is false the if block will not execute.

    if statement syntax:
    if(condition)
    {
    //if block
    // Statements to execute if
    // condition is true
    } //outside the if block

    Example:

    #include <iostream>
    using namespace std;
    int main()
        {
             int i=7;
             
             if (i<10)
                {
                      //if block
                     cout<<"7 is less than 10";
                     cout<<endl;
                }
             cout<<"Outside the If Block";
         }

    Output:

    7 is less than 10
    Outside the If Block

    Behind the code In the above example the condition of if (i <10) was true so the statement inside the if block get executed.

    2. if-else statements

    else statement is the extension of if statements, we cannot use else statement by itself if we do not have an if statement. There should always be an if statement preceding the else statement. If the condition of if statement gets false then only the code of else block gets executed. The if-else statements often used when we want to decide between two blocks of code based on a single condition. Syntax:

    if (condition)
    
    {
        // Executes this block if
        // condition is true
    }
    
    else
    {
        // Executes this block if
       // condition is false
    }

    Example:

    #include <iostream>
    using namespace std;
    int main() 
      {
        int a = 20, b=30;
        if (a > b)
          {
             cout<<"a is greater than b";
          }
        else
          {
             cout<<"b is greater than a";
          }
       return 0;
      }

    Output:

    b is greater than a

    Behind the code: In the above example, the value of a is 20 and b is 30. The condition of if statements check whether a>b or not. 20 is less than 30 so the condition of if statement becomes false , so the code of else block get executed.

    3. Nested if

    In C++ programming if we use an if statement inside another if statement then it would be known as nested if statements.

    Syntax:
    if (condition1)
       {
           // Executes when condition1 is true
           if (condition2)
               {
                   // Executes when condition2 is true
               }
       }

    Example:

    #include <iostream>
    using namespace std;
    int main() 
        {
           int a = 12;
           if (a==12)
               {
                    if(a<20) //it is a nested if statement because it is inside the if(a==12) statement
                         {
                           cout<<"a is less than 20";
                          }
               }
    return 0;
    }

    Output:

    a is less than 20

    Behind the code: In this example of nested if we have two if statements if(a==12) and if(a<20) . Here the if(a<20) statement is the nested-if statement because it is inside the if(a==12) statement block.

    4. if-else if statements

    The if-else if statement is an extension of if-else statements. We use this statement used when we have to choose one from the multiple options, that’s why in this statement we can check for multiple conditions.

    Syntax:
    if (condition)
        statement;
    else if (condition)
        statement;
    .
    .
    else
        statement;

    Example

    #include <iostream>
    using namespace std; 
    
    int main() {
        int a = 12, b=10, c= 30; 
    
        if (a>b && a>c)
          {
             cout<<"a is grerater than b and c";
          }
        else if(b>a && b>c)
          {
             cout<<"b is greater than a and c";
          }
        else // if all the above condition become false than only else statement execute
          {
             cout<<"c is greater than a and b";
          }           
        return 0;    
    
    }

    Output:

    c is greater than a and b

    Behind the code: In this example the condition of i f (a>b && a>c) and else if(b>a && b>c) became false so their blocks did not execute, and we know that if all the conditions become false the else statement block executes.

    5. Switch case

    Switch case is a substitution of long if statements, we do not use switch cases so often, because it has a rigid structure and if we want to use a switch case so for all its cases the data type should be same. Switch case requires some keywords to perform the function, such as case, break and default. The case keyword signifies the condition is true or not, the break halts the switch execution flow and default statement block execute if all the case if the switch becomes false . Syntax: switch (n)

    {
        case 1: // code to be executed if n = 1;
            break;
    
        case 2: // code to be executed if n = 2;
            break;
        
        default: // code to be executed if n doesn't match any cases
    }

    Example

    #include <iostream>
    using namespace std; 
    
    int main() {
    
      int weekday = 3;
    
        switch (weekday)
        {
            case 1: 
                cout << "It's Sunday";
                break;
    
            case 2: 
                cout << "It's Monday";
                break;
    
            case 3: 
                cout << "It's Tuesday";
                break;
    
            case 4: 
                cout << "It's Wednesday";
                break;
    
            case 5: 
                cout << "It's Thursday";
                break;
    
            case 6: 
                cout << "It's Friday";
                break;
    
            case 7: 
                cout << "It's Saturday";
                break;                                     
    
            default: 
                cout << "Not Found";
                break;  
        }   return 0;    
    
    }

    Output:

    It's Tuesday

    Behind the code: The switch(weekday) statement matches the value of weekday with every case value and it matched with case 3 that’s why the statement of case 3 get executed.

    Quick Summary

    • In C++ we use if, if-else, if-else if and switch statements for the decision-making structure.
    • if statements used when we have to choose whether we what to execute a statement or not.
    • If-else used when we have to choose between two options.
    • If-else if used when we have multiple options to choose from.
    • The switch statement is used as a substitute of long if

    People are also reading: