C++ Decision Control Statements
In C++ programming, decision control statements perform different computations or actions depending on whether a specific condition holds true or false. Based on it, certain decisions are to be made. They are also called conditional statements.
We have the following decision control statements in C++:
- if statement – we use this statement if we want to execute some code only if a specified condition is true
- if-else statement – we use this statement if we want to execute some code if the condition is true and another code if the condition is false
- if-else nesting statement – we use this statement if we want to select one of many blocks of code to be executed
- switch-case statement – we use this statement if we want to select one of many blocks of code to be executed
1. if statement
We should use the if statement if we want to execute some code only if a specified condition is true.
Syntax
if (condition)
{
code to be executed if condition is true
}
Example 1.
//decision01.cpp
#include <iostream>
using namespace std;
int main() {
int a = 20, b = 10;
if(a > b) {
cout << a << " is bigger than " << b;
}
return 0;
}
Output:
20 is bigger than 10
2. if-else statement
If we want to execute some code if a condition is true and another code if the condition is not true, use the if-else statement.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example 2.
//decision02.cpp
#include <iostream>
using namespace std;
int main() {
int n = 10;
// testing for even or odd number
if(n%2 == 0) {
cout << n << " is an even number.";
}
else {
cout << n << " is an odd number.";
}
return 0;
}
Output:
10 is an even number.
3. if-else nesting statement
We should use the if-else nesting if we want to select one of many sets of lines to execute.
Syntax
if (condition1) {
code to be executed if condition1 is true
}
else if (condition2) {
code to be executed if condition2 is true
}
else {
code to be executed if condition1 and condition2 are not true
}
Example 3.
//decision03.cpp
#include <iostream>
using namespace std;
int main() {
int a = 30, b = 30;
if(a > b) {
cout << "a is bigger than b.";
}
else if(a < b) {
cout << "a is smaller than b.";
}
else {
cout << "a and b are equal.";
}
return 0;
}
Output:
a and b are equal.
4. switch-case statement
We should use the switch-case statement if we want to select one of many blocks of code to be executed.
Syntax
switch(choice)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
case 3:
execute code block 3
break;
default:
execute code if choice is different
break;
}
This is how it works: First we have a single expression choice (most often int or char type variable is used), that is evaluated once.
The value of the expression is then compared with the values for each case in the structure.
If there is a match, the block of code associated with that case is executed.
We use break statement to prevent the code from running into the next case automatically.
Example 4
//decision04.cpp
#include <iostream>
using namespace std;
int main() {
int choice = 3;
switch(choice) {
case 1:
cout << "hello";
break;
case 2:
cout << "hi";
break;
case 3:
cout << "welcome";
break;
default:
cout << "bye";
break;
}
return 0;
}
Output:
welcome