Categories: C++

Decision-Making in C++

In real life, we face situations where we must make decisions and decide what to do next. Similarly, the programmer must specify one or more conditions to be evaluated or tested by the program, as well as a statement or statements to be executed if the condition is determined to be true, and optionally, additional statements to be executed if the condition is determined to be false.

Decision-making statements available in C++ are:

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

The if statement is the most straightforward decision-making statement. It is used to determine whether or not a specific statement or block of statements will be executed, i.e. if a certain condition is true, then a block of statements is executed, otherwise not.

Syntax:

if(condition) 
{   // condition is true
   // execute Statements 
   
}
// condition is false
   // execute Statements 
Flowchart:
Example:

#include <iostream>
using namespace std;
  
int main()
{
    int i = 1;
  
    if (i < 0) {
        cout << "Negative Number";
    }
  
    cout << "positive Number";
}
Output:
positive Number
if-else statements:

When the condition is false, we can use the else statement in conjunction with the if statement to execute a block of code.

Syntax:

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

#include <iostream>
using namespace std;
  
int main()
{
    int age=21;
    cout<<"Enter Age: ";
  
    if(age>18)
    cout<<"Person is an adult for voting.";
    else
    cout<<"Person is underage for voting.";
    return 0;

}
Output:
Person is an adult for voting.
Nested if-else statements:

The nested if-else means an if-else statement within an if or else block. Let’s take an example for better understanding:

Example:
#include <iostream>
using namespace std;
int main( )
{
    int a,b,c;
    cout << "enter 3 number";
    cin >> a >> b >> c;
    if(a > b)
    {
        if( a > c)
        {
            cout <<a<< " is greatest";
        }
        else 
        {
            cout <<c<< " is greatest";
        }
    }
    else
    {
        if( b> c)
        {
            cout <<b<< " is greatest";
        }
        else
        {
            cout <<c<< " is greatest";
        }
    }
    return 0;
}
else-if Ladder:

A user can select from several options here. When one of the if conditions are met, the statement associated with that if is executed, and the rest of the else-if ladder is skipped. If none of the conditions are met, the last else statement is executed.

Syntax:

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

else
    statement;
Example:

#include <iostream>
using namespace std;

int main()
{
 int i = 90;

 if (i == 10)
  cout << "i is 10";
 else if (i == 15)
  cout << "i is 15";
 else if (i == 20)
  cout << "i is 20";
 else
  cout << "i is not present";
}
Output:
i is not present
Key points:
  • The group of statements within the if curly braces (excluding the else) forms an if block.
  • The group of statements within the else curly braces creates an else block.
  • The else is written just below the if. The statements in the if block and those in the else block are indented to the right.
  • If there is only one statement in the else block to be executed, we could drop the pair of curly braces.

Note: also read about the C++ sizeof() and typedef Operator

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago