PREV NEXT
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"; } |
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; } |
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; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
switch(condition) { case constant expression: statement; break; //it is optional case constant expression: statement; break; //it is optional //you can have any number of cases default: //optional statement; } |
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; } |