Let there are multiple statements in the code. Now we want to provide an ability to the programmer to decide what statements to be executed and what statements should not be executed depending upon the specific condition. This is known as decision making.
C++ provided following decision making statements
1. If statement
2. If-else statement
3. Nested if-else statement
4. Switch statement
5. Nested switch statement
6. Conditional ? : Operator
If Statement
Based on a condition, if statement allows us to control a program whether to execute specific statement or not. Condition can be true or false.
Below is the syntax of if statement:
if (condition) statement
OR
if (condition) { Statements }
The condition is checked and if it is true then the statement will be executed.
Now we will write the program that will output all even numbers in the range from 0 to 100. But before writing the code let’s discuss the algorithm for it. We need to start from 0 so we will initialize X with 0. We have to find even numbers till 100 so we will write a loop till 100.
We have to find numbers from 0 to 100 so we will increment X. Now if X is divisible by 2 then we will print it else we will skip the print. So we will write it inside if block by putting if condition X%2==0 (this checks if X is divisible by 2, if yes then it will return true).
Below is an algorithm
Initialize Integer X with value 0 FOR X = 0 to 100 IF X % 2 == 0 Print X ENDIF ENDFOR
Below is the code for printing only even numbers from 0 to 100.
cout << "Even numbers" << endl; for(int x = 0; x <= 100; ++x) { if (x % 2 == 0) cout << x << endl; }
Output of above code will print: all even numbers from 0 to 100.
If-Else Statement
The if statement can be supplemented with else statement:
if (condition) statement1 else statement2
OR
if (condition) { statements1 } else { statements2 }
This means that if condition is true then only statement1 will be executed, but if the condition is false then only statement2 will be executed.
We can modify our above program by displaying whether the number is even or odd. The pseudo code for this task can look in the following way:
Initialize Integer X with value of 0
FOR X = 0 to 100 IF X % 2 == 0 Print X Is even ELSE Print X is odd ENDIF ENDFOR
for(int x = 0; x <= 100; ++x) { if (x % 2 == 0) cout << x << " is even" << endl; else cout << x << " is odd" << endl; }
Switch Statement
The if/else statement provides only two variants of actions. But often there are situations that we have to make decisions not only on true/false statements. For this purpose we can use switch statement:
switch (expression) { case constant1: group-of-statements-1; break; case constant2: group-of-statements-2; break; . . . default: default-group-of-statements }
Switch statement evaluates the expression. If expression is equal to constant1, the group-of-statements-1 is performed, if expression is equal to constant2, the group-of-statements-2; is performed etc. If the expression is not equal to all the case constants – the default-group-of-statements is executed.
Here is the example of the program that allows more than 2 decisions made according to the expression’s value:
for (int hour = 0; hour < 13; ++hour) { switch (hour) { case 2: cout << " It is 2 o'clock" << endl; break; case 5: cout << " It is 5 o'clock" << endl; break; default: cout << " I do not know" << endl; break; } }
I do not know
I do not know
It is 2 o’clock
I do not know
I do not know
It is 5 o’clock
I do not know
I do not know
I do not know
I do not know
I do not know
I do not know
I do not know
This program examines the value of the hour variable in a cycle. When hour is equal to 2, it displays the corresponding time. When hour is equal to 5 it displays the message too. In other cases it display the message “I do not know”.