Decision Making in C++

Decision Making

 

Here, in this section we will discuss about decision making in C++. Decision making is about deciding which statement should be executed and which statement should be skipped from execution  based on certain conditions.

decision making

Methods of decision making in C++

C++ decision making can be done via the following –

  • if statement
  • if-else statements
  • if-else-if ladder
  • nested if statements
  • switch statements
  • Jump Statements:
    • break
    • continue
    • goto
    • return

1. If statement

It’s one of the most basic statements. We test if a given condition is true. If it is then we implement some lines of code. Following is the syntax for the same.

if(condition) 
{
   // Following statement(s) will be executed
   // if condition is true
}
Decision making in C++ new

Example

#include<iostream>
using namespace std;

int main()
{
    int a = 20, b = 10;
    
    if (a > b)
    {
        cout << "a is greater than b" << endl;
    }
    
    cout << "Outside if loop now";
}

Output

a is greater than b
Outside if loop now

2. if-else Statement

  • If a given condition is true we implement some lines of code.
  • If a given condition was not true we implement some other lines of code

Following is the syntax for the same.

if(condition) { 
    // Following statement(s) will be executed 
    // if condition is true 
}
else{
    // Following statement(s) are executed
    // if condition was false
}

Example

#include<iostream>
using namespace std;

int main()
{
    int a = 20;
    
    if (a > 100)
    {
        cout << "a is greater than 100" << endl;
    }
    else{
        cout << "a is smaller than 100" << endl;
    }
}

Output

a is smaller than 100

3. If-else-if Ladder

We can have multiple conditions checking to do this we use if-else-if ladder. Following is the syntax for the same –
if(condition){
    // execute these statements if true
}
else if(condition){
    // execute these statements if true
}
else if(condition){
    // execute these statements if true
}
else{
    // execute this if none of above are true
}
Decision making in C++ new – 1

Example

#include<iostream>
using namespace std;

int main()
{
    int num = -20;
    
    if(num == 0){
        cout << "Number is Zero"; } else if(num > 0){
        cout << "Number is positive";
    }
    else{
        cout << "Number is negative";
    }
}}

Output

Number is negative

4. Nested if-else in C++

If else conditions can be nested within one another as well.

if (condition1) 
{
   // Executes when condition1 is true
   if (condition2) 
   {
      // Executes when condition2 is true
   }
}
Decision making in C++ new – 2

Example

#include<iostream>
using namespace std;

int main()
{
    int num = 20;
    
    if(num >= 0){
        if(num == 0){
            cout << "Number is Zero";
        }
        else{
            cout << num << " is positive";
        }
    }
    else{
        cout << num << " is negative";
    }
}

Output

20 is positive

5. Switch Case

It is ideal for whenever we can to merge a lot of if – else loops into multiple cases checking and execute unique statements if one of them is true. 

switch(expression) 
{
   case constant-expression1:
      // statement(s);
      break;
   
   case constant-expression2:
      // statement(s);
      break;
  
   // you can have any number of case statements
   default: // Optional but ideal
      // statement(s);
}
Decision making in C++ new – 3

Example

#include<iostream>
using namespace std;

int main()
{
  char op;
  int num1 = 20, num2 = 5;
  
  cout << "Enter operator either + or - or * or /: "; cin >> op;

  switch(op)
  {
    case '+': 
        cout << num1+num2;
        break;
    case '-': 
        cout << num1-num2;
        break;
    case '*': 
        cout << num1*num2;
        break;
    case '/':
        cout << num1/num2;
        break;
    
    //if case label not available example op = %
    default: 
        cout << "Select valid choice";
        break;
    }
}

Output

Enter operator either + or - or * or /: *
100

Jump Statements

Now we will discuss jump statements which are 

  • Jump Statements:
    • break
    • continue
    • goto
    • return

Knowledge for looping like – For, while, do while is important. Make sure that you visit C++ Loop Types before moving the topics below.

Break Statement

Whenever a break statement is encountered inside a loop (for or While or Do-while), the loop is immediately terminated and program control resumes at the next statement following the loop

for(init;conditions;increments){
    
    if(some condition(s)){
        // For loop immediately terminates
        // no matter if there are pending iterations
        break;
    }
}
// program flow will come here immediately
Decision making in C++ new – 4

Example

#include<iostream>
using namespace std;

int main()
{
    // ideally should run 10 times b/w [1,10]
    for(int i = 1; i <= 10; i++){
        
        // whole loop terminates when i becomes divisible by 5
        if(i % 5 == 0)
            break;
    
        cout << "i: " << i << endl;
    }
}

Output

i: 1
i: 2
i: 3
i: 4

Continue Statement

Whenever a continue statement is encountered inside a loop, the current iteration of the loop is skipped and the loop executes from the next iteration

for(init;conditions;increments){
    
    if(some condition(s)){
        // Current iteration of loop is skipped
        // Starts from next iteration
        break;
    }
}
// program flow will come here after loop completion

Example

#include<iostream>
using namespace std;

int main()
{
    // ideally should run 10 times b/w [1,10]
    for(int i = 1; i <= 10; i++){
        
        // Even iterations are skipped
        // it true then continue statement forces
        // loop to be started from next iteration
        if(i % 2 == 0)
            continue;
        
        // statements skipped if continue statement executes
        cout << "i: " << i << endl;
    }
}

Output

i: 1
i: 3
i: 5
i: 7
i: 9

Goto Statement

It is a unique type of jump statement which is sometimes also called an unconditional jump statement. Which is used to jump from anywhere to anywhere within a function.

Let us look at a sample goto program to understand more about –

Decision making in C++ new – 6

Example

#include<iostream>
using namespace std;
  
// function to check even or not
void checkNum(int num)
{
    if (num > 0)
    // jump to positive
        goto positive; 
    else if(num < 0)
    // jump to negative
        goto negative; 
    else
    // jump to zero
        goto zero;
  
positive:
    cout << num << " is positive";
    // return if positive
    return; 
negative:
    cout << num << " is negative";
    // return if negative
    return; 
zero:
    cout << num << " is zero";
}

int main()
{
    int num = -12;
    checkNum(num);
    return 0;
}

Output

-12 is negative

Return Statement

Return statement helps us to return from the execution flow of a program from wherever the return statement is encountered. It may or may not need any conditional statement.

As soon as we see return statement the program flow is returned to wherever the function was called from. A void function may not have a return type but for all others the return type must be a specified datatype.

Example

#include<iostream>
using namespace std;
 
// Here we have a non void return type
// function to calculate multiplication of two numbers
int multiply(int a, int b)
{
    int mul = a * b;
    return mul;
}
 
// returns void
// function to display result
void display(int val)
{
    cout << "The product is : "<< val;
    return;
}
 
int main()
{
    int num1 = 15;
    int num2 = 4;
    
    int result = multiply(num1, num2);
    display(result);
    
    return 0;
}

Output

The product is : 60