C++ Decision Control Statements

WHAT ARE DECISION CONTROL STATEMENTS?

The decision control statements are the decision making statements that decides the order of execution of statements based on the conditions. In the decision making statements the programmer specify which conditions are to be executed or tested with the statements to be executed if the condition is true or false.

 SOME OF THE DECISION CONTROL STATEMENTS ARE

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

Let us discuss all these statements briefly.

IF STATEMENT

The if statement consists a condition which is followed by one or some of the statements, if the condition is true then the statements will be executed or else not. This statement is the simple and easy decision control statement.

LET US HAVE A LOOK AT THE SYNTAX:

1
2
3
4
5
6
7
if (condition)
 
{
 
//statements to be executed if condition is true
 
}

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
 
{ int a = 10;
 
if (a > 20)
 
{ cout<<“10 is less than 20”;
 
}
 
cout<<“Out of if block”;
 
}
 
Output:

Out of if block

IF – ELSE STATEMENT

In the if-else statement the if statement is followed by the else statement which will execute when the expression is false.

LET US HAVE A LOOK AT THE SYNTAX:

1
2
3
4
5
6
7
8
9
10
11
if (condition)
 
{    //statement will execute if the condition is true
 
}
 
else
 
{   //statement will execute if the condition is false
 
}

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main () {
 
int x = 30; //variable declaration
 
// check the condition
 
if( x < 20 ) {
 
cout << “x is less than 20;” << endl; // if condition is true then print the following
 
} else {
 
cout << “a is not less than 20;” << endl; // if condition is false then print the following
 
}
 
cout << “The value of x is : “ << x << endl;
 
return 0;
 
}
Output :
a is not less than 20;
the value of X is : 30 

We can have the nested if statements also with else statements comparing the different conditions.

LET US UNDERSTAND THIS THROUGH AN EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;
int main( )
 
{
 
int x =4, y =6, z=10;
 
cout << “\nThe 3 numbers are:”<<x<<“\t”<<y<<“\t”<<z;
 
if(x > y)
 
{
 
if( x > z)
 
{
 
cout << “\nx is greatest“;
 
}
 
else
 
{
 
cout << “\nz is greatest“;
 
}
 
}
 
else
 
{
 
if( y > z)
 
{
 
cout << “\ny is greatest“;
 
}
 
else
 
{
 
cout<<“\nz is greatest;
 
}
 
}
 
return 0;
 
}
Output: 
The 3 numbers are  4               6              10
z is greater

SWITCH STATEMENT

The switch statement allows the variable to be tested for equality against a list of values. In the switch statement every value is called case and the variable is switched on is checked for each case.

LET US HAVE A LOOK AT THE SYNTAX:

LET US HAVE A LOOK AT THE EXAMPLE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
 
int main () {
 
char grade = ‘c’; // local variable
 
switch(grade) {
 
case ‘a’ :
 
cout << “Excellent!” << endl;
 
break;
 
case ‘b’ :
 
case ‘c’ :
 
cout << “Well done” << endl;
 
break;
 
case ‘d’ :
 
cout << “You passed” << endl;
 
break;
 
case ‘e’ :
 
cout << “Need Improvement” << endl;
 
break;
 
default :
 
cout << “Invalid grade” << endl;
 
}
 
cout << “Your grade is “ << grade << endl;
 
return 0;
 
}
Output : 
Well done.
Your grade is C