Home » Decision-Making Statements In C++ (if, if-else, nested if statement, and more)

Decision-Making Statements In C++ (if, if-else, nested if statement, and more)6 min read

by TechABU
Decision Making Statements in C++

Control flow statements, also known as flow control statements, are supported by C++, enabling programmers to manage the order in which processors execute statements in a program. 

Decision, repetition, and compound statements are the three types of control flow statements that are used the most.

What are Decision Statements?

While coding in C++, a programmer might need to execute one statement or a group of statements if a certain condition is true and the other statement or group of statements if the certain condition is false.

This is only possible in a C++ program by using decision statements.

The following are different types of decision statements.

#1. if statement:

The if statement is the most basic conditional statement used in C++.

The if statement is used to skip or execute a statement or group of statements by checking the condition.

The condition for if statements is usually a relational expression.

The statement or group of statements that follow the if statement is executed if the condition is true.

The statement or group of statements that follow the if statement is not executed if the condition is false.

There are two forms of the if statement:

  • Single if statement
  • Multiple if statement

The single if statement:

The single if statement only has one statement.

The syntax of the single if statement,

if (condition)
	statement;

The multiple if statement:

The multiple if statement contains a group of statements. The statements in the multiple if statement form are written between curly braces { }.

The syntax of multiple if statements,

if (condition)
{
	statement1;
	statement2;
	.
	.
	.
	statementN;
}

flowchart of if statement in C++
flowchart of if statement in C++

C++ Program Example for if statement:

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
	int marks;
	cout << "Enter your Marks = ";
	cin >> marks;
	
/* now using the decision-making statement to execute or skip a statement or group of statements. */

	if (marks > 40)
	cout << " Congrats! You are Passed. ";

	getch();
	return 0;
}
OUTPUT:
Enter Your Marks = 21
Congrats! You’re Passed.

Now, let’s understand what happened in the above C++ program.

Okay, so we’ve taken a variable, marks. We’ve asked the user to input their marks in the next statement.

After that, we’ve used the decision-making statement (if statement) to execute the statement “Congrats! You’re Passed.” only if the condition is true.

So let’s say the user has entered the number 41, right? So when the compiler reaches the if statement, it will check the condition.

In this case, the condition is true because the number 41 is greater than 40.

So all the statements after the condition will be executed. In the example, we’ve used one statement, “Congrats! You’re Passed.” that will be executed.

But,

The condition would be false if the user entered a number less than 40. So, in that case, all the statements after the condition will be skipped, and the program will be terminated.

This is the basic use of the if statement. We can use it to execute or skip statements by applying some conditions.

#2. if-else statement:

The if-else statement is the same as the if statement. 

The only difference is that “the if-else statement executes a statement or group of statements if the condition is true, and the other statements if the conditions are false.”

In any case, a statement or a block of statements is executed, and the other is skipped. 

In other words;

  • Both blocks of statement can never be executed.
  • Both blocks of statements can never be skipped.

There are also two forms of the if-else statement:

  • Single if-else statement
  • Multiple if-else statements

The single if statement:

The single if-else statement only has one statement.

The syntax of the single if statement,

if (condition)
	statement;
else
	statement;

The multiple if statement:

The multiple if-else statements contain a group of statements. The statements in the multiple if-else statement form are written between curly braces { }.

The syntax of multiple if statements,

if (condition)
{
	statement1;
	statement2;
	.
	.
	.
	statementN;

else

	statement1;
	statement2;
	.
	.
	.
	statementN;
}

if-else statement flowchart
if-else statement flowchart

C++ Program Example for if-else statement:

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
    int marks;
    cout << "Enter your Marks = ";
    cin >> marks;
    
/* now using the decision-making statement to execute or skip a statement or group of statements. */

    if (marks > 40)
    cout << " Congrats! You are Passed. ";
    
    else
    
    cout<< " Oops! You're Failed. ";
    
    getch();
    return 0;
}
OUTPUT (Case 1):
Enter your Marks = 51
 Congrats! You are Passed.
OUTPUT (Case 2):
Enter your Marks = 21
 Oops! You're Failed.

Here in this example, we’ve used the same program we wrote for the if statement. We’ve just modified it a little.

In the if statement, the condition should have to be true to execute the statements, and if the condition is false, all the statements are skipped.

But in the if-else statement, if the condition is true, one block of the statements will be executed, and the other will be skipped.

Let’s say the user enters their marks as 47 (greater than 40), then the statement after the if statement “Congrats! You’re passed.” will be executed, and the other statement “Oops! You’re Failed.” will be skipped.

And if the user enters his marks less than 40 (for example, 31), then the first statement, which is “Congrats! You’re passed.” will be skipped, and the other statement, which is “Oops! You’re Failed.” will be executed.

As you’ve seen, in any situation, one statement block is executed, and the other part is skipped.

I hope you’ve understood the basic use of the if and if-else statements in C++.

#3. Nested ‘if’ statement:

An if statement within an if statement is a so-called Nested ‘if’ statement. In C++, Yes! It is possible to chain if-else statements together.

In nested if statements, control only goes into the inner if when the outer condition is satisfied.

Only one block of statements is executed, and the remaining blocks are skipped automatically.
Note: A nested ‘if ladder’ is created when one if-else statement is used inside the body of another if-else structure.

nested if statement flowchart
nested if statement flowchart

C++ Program Example for Nested if statement:

// C++ program to find whether you're a child, teenager, or an adult
// using nested if statements

#include <iostream>
#include <conio.h>
using namespace std;

int main() 
{

  int age;
  cout<"Enter your age= ";
  cin>>age;
if(age>14)
{
  if(age>=18)
 {
    cout<<"Adult";
 }
  else
 {
    cout<<"Teenager";
 }
}
else
{
  if (age > 0)
 {
    cout<<"Child";
 }
  else
 {
    cout << "Something's wrong";
 }
}
getch();
return 0;
}
OUTPUT:
Enter your age= 19
Adult

Jump Statements in C++

Programmer sometimes needs to use such statements in a program responsible for the jump from one statement of the program to another statement, or we can say one part of a program to another.

The most commonly used jump statements in C++ language are:

  • break statement
  • goto 
  • continue

break statement:

The break statement is used in a C++ program to jump from one part of the program to another. 

To properly end a case clause in a switch-default structure and to leave a loop in a loop structure, the break statement is often used in both situations.

We will discuss the use of break statements in switch-default cases later in this article, and the use of break statements in loops will be discussed when we will cover loops in C++.

goto statement:

The goto statement in C++ programming is used to change the typical flow of execution of a program by shifting control to another part of the program.

Syntax of goto statement:

The general syntax of the goto statement is given below:

goto label;
... .. ...
... .. ...
... .. ...
label: 
statement;
... .. ...

In the syntax above, the label is an identifier. The program’s control goes to label: and executes the code there when the goto label; is encountered.

We will discuss the goto statement in a separate article; for now, you just need to know that the goto statement is not required in every C++ program, and it is generally recommended to avoid using it. 

continue statement: 

The continue statement offers a straightforward method to move to the beginning of the next iteration by passing the remaining loop portion for an iteration.

#4. Switch-default Statement:

The switch-default statement is considered the best alternative to the Nested if statement.

It can be more challenging to determine which if clause belongs to which else clause in an if-else structure that is nested.

The switch statement checks a range of potential constant values for an expression before executing those that match the particular label or case constant value.

Syntax of the switch-default statement:

switch (expression)
{
	
case constant_1:
	statements;
break;
case constant_2:
	Statements;
break;
.
.
.
default:
Default group of statements;

}

C++ Program Example for Switch-default statement:

//C++ program to make a calculator using switch statement

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	int x, y;
	char op;
	cout<< " Enter First Integer= ";
	cin>>x;
	cout<< " Enter Second Integer= ";
	cin>>y;
	cout<< " Enter an Operator= ";
	cin>>op;
	
	switch (op)
	{
		case '+':
			cout<< " \n\nSum of two integers= "<<x+y;
		break;
		
		case '-':
			cout<< " \n\nSubtraction of two integers= "<<x-y;
		break;
		
		case '*':
			cout<< " \n\nMultiplication of two integers= "<<x*y;
		break;
		
		case '/':
			cout<< " \n\nDivision of two integers= "<<x/y;
		break;
		
		default:
			cout<<" \n\n\aOops! Invalid Operator is Entered. Please try again!";
			exit(0);
	}
	getch();
	return 0;
}
OUTPUT:
Enter First Integer= 20
Enter Second Integer= 10
Enter an Operator= +


Sum of two integers= 30

In the above example, the switch will check the expression:

  • If the value of the expression is equal to ‘+,’ it will execute all the statements of case ‘+’ until it encounters a break statement. When it reaches the break statement, the program jumps to the end of the switch.
  • If the value of the expression is not equal to case ‘+,’ and it is matched with the case ‘*,’ the statement or group of the statement of case ‘*’ will be executed until the break keyword is reached. Then a jump will occur to end the switch case.
  • Finally, if the expression value does not match the constants, the program executes the statements after the default keyword.

Leave a Comment

RECOMMENDED ARTICLES

Send this to a friend