Role And Importance Of Decision Making Constructs In C++.
In programming, it is required that we just don’t keep on executing the statements one by one. Rather there will be a situation when we have to check some conditions and accordingly, if the condition is true or false, we need to execute a different block of statements.
In other words, while programming, we need to take care of decision making as well. In this tutorial, we will explore the decision-making constructs in C++.
=> Take A Look At The C++ Beginners Guide Here.
Table of Contents:
Decision Making In C++
Like other programming languages, C++ also has various decision-making constructs which aid the programmer in decision making, as a result of which the programmer can make correct decisions and execute the appropriate block of statements to achieve the desired results.
Next, we will discuss the important decision-making constructs present in C++.
If Statement
The general syntax of ‘If’ statement is given below:
if(condition) { statement _block; }
This is the basic decision-making construct. The condition inside the ‘if’ construct is evaluated. If it is true the statement block inside the braces following the ‘if’ statement is executed.
If the condition is false, then the statement block is skipped and control is passed to the statements following the ‘if’ block.
The flow diagram for the ‘if’ statement is given below.
Let us take an example to demonstrate the ‘if’ decision construct.
#include <iostream> #include <string> using namespace std; int main() { int num; cout<<"Enter number between 1 and 10:"; cin>>num; if(num>=1 && num<=10) { cout<<"\nYou have entered correct number"; return 0; } cout<<"\nNumber not between 1 and 10"; }
Output:
Enter number between 1 and 10:4
You have entered the correct number
In the above program, we make use of ‘if’ construct to determine if the number entered is between 1 and 10.
if-else Statement
Sometimes a situation arises where we want to execute one statement block if the condition is true and another statement block is the condition is false. In such a situation, we make use of ‘If-else’ construct.
The general syntax for the same is given below.
if(condition) { statement _block; } else { statement _block; }
If the condition is true, then the statement block following the ‘if’ condition is executed. If the condition is false, then the statement block following the ‘else’ statement is executed.
Flow diagram for the ‘if-else’ construct is shown below.
To understand the ‘if-else’ statement, let us consider the following Example.
#include <iostream> #include <string> using namespace std; int main() { char mychar = 'S'; if(mychar == 'S') cout<<"\nCharacter is 'S'"; else cout<<"\nCharacter is not 'S'"; }
Output:
Character is ‘S’
In this program, we will check if the character is ‘S’ and accordingly we print the message.
Else if Statement
In this construct, we have multiple conditions as well as options. If the condition is true, then the statement block following ‘if’ statement is executed. If the condition is false, then another if the condition is checked.
Depending on whether the condition is true or false, the statement block or another condition is checked. This is shown in the general syntax of this construct below.
General Syntax:
if(condition) { statement-block1; }else if(condition) { statement-block2; } else if(condition) { statement-block3; } else default-statement;
As shown above, there are multiple ‘if’ statements in the else part of the first ‘if’ statement. The last level in the construct is the ‘else’ statement which executes the default statement.
Flow Diagram of Else if Statement
Let us see an example of this construct.
#include <iostream> #include <string> using namespace std; int main() { int num; cout<<"To check if input number is positive or negative"<<endl; cout<<"Enter the number: "; cin>>num; if(num==0) { cout<<"The number entered is 0"<<endl; } else if(num < 0) {cout<<"The number is negative"<<endl;} else {cout<<"The number is positive"<<endl;} return 0; }
Output:
To check if input number is positive or negative
Enter the number: 7
The number is positive
In the code shown above, we make use of the ‘else-if’ construct. Here we check if the number entered is zero, negative or positive. For this, we make use of the if-else if-else construct.
Nested If Statement
By term ‘Nested If’, we mean one or more ‘if’ statements inside another ‘if’ statement. We can have any form of ‘if’ statement like simple if, if-else, else-if or another nested if depending on our requirements.
Given below is the general syntax of the nested if and also the flow diagram.
General Syntax:
if(condition) { if(condition) { Statement_block; } else{ Statement_block;} } else {Statement_block;}
Flow Diagram of Nested If Statement:
Let us see an example that demonstrates the Nested-if Statement:
#include <iostream> #include <string> using namespace std; int main() { int num=10; if(num>0) { if(num==10) cout<<"Number = 10"<<endl; else if(num > 10) cout<<"Number is greater than 10"<<endl; else if(num < 10) cout<<"Number is less than 10"<<endl; } else cout<<"Number is negative"<<endl; return 0; }
Output:
Number = 10
Switch Statement
As our programs get more complex, we tend to have more and more conditions to test, and if we still use ‘if’ statements, there will be numerous ‘if’ statements which will be difficult to handle and thereby make our program unreadable.
In order to avoid this, we have a new construct named ‘Switch statement’ which allows us to test a condition and have multiple options/cases that can change the flow of execution.
The switch statement evaluates a conditional expression and then matches the value of this expression to each case. When the expression value matches the case, the statement code of this case is executed.
The general syntax for the switch statement is given below:
switch(constant_expression){ case 1: {statement_block; break;} case 2: {statement_block; break;} .. case n: {statement_block; break;} default: {statement_block; break;} }
As shown in the above syntax, the switch conditional expression should be the one that evaluates to a constant value. Expressions evaluating variable values are not valid. We have ‘n’ cases for the switch statement and a default case. Note that the default case will be executed when the expression does not match any of the cases provided.
The default case is optional. In case, the default case is absent, the switch will exit when no case is matched.
The statement ‘break;’ is also optional. The break statement is used to exit out of the statement. In case if the break statement is not provided, after a match for the case is found, then other subsequent cases will also be executed.
The flow diagram for the Switch Statement is given below:
Following is an example program for the Switch Statement.
#include <iostream> #include <string> using namespace std; int main() { char mychar; cout<<"Enter an uppercase vowel character:"; cin>>mychar; switch(mychar) { case 'A': {cout<<"Character entered is A"<<endl;break;} case 'E':{cout<<"Character entered is E"<<endl;break;} case 'I':{cout<<"Character entered is I"<<endl;break;} case 'O':{cout<<"Character entered is O"<<endl;break;} case 'U':{cout<<"Character entered is U"<<endl;break;} default:{cout<<"Vowel was not entered"<<endl;} } return 0; }
Output:
Enter an uppercase vowel character: U
Character entered is U
In the above program, we check if the character entered is an English vowel. For this, we have specified the vowel cases and a default case if in case a character other than vowel is entered.
Goto Statement
We use ‘goto’ statement, whenever we need to branch to another part of the code from one point. Here we label a particular statement and then using ‘goto label’ we branch to that particular statement.
The general syntax for the goto statement is:
goto label; …. …. label: statement_block; …. ….
The flow diagram for the Goto Statement is shown below:
The ‘goto’ statement allows us to jump to any part of the program. However, the use of ‘goto’ is considered to be a bad programming practice and we should avoid it as much as possible.
The main reason is jumping to other parts of the code using ‘goto’ statement can make our program more complex and unreadable. Instead, we can write far efficient programs using the break and continue statements.
Let us take an example of the ‘goto’ statement.
#include <iostream> #include <string> using namespace std; int main() { int num = 5; if(num>0) { cout<<"Number is greater than 0"<<endl; if (num <= 5) goto labelfive; else if (num >5) cout<<"Number is greater than 5 too"<<endl; } labelfive: if(num == 5) cout<<"Number is equal to 5"<<endl; else if(num < 5) cout<<"Number is less than 5"<<endl; return 0; }
Output:
Number is greater than 0
Number is equal to 5
In the above program, we make use of ‘goto’ statement. We check if the number is less than or equal to 5 and if it is, we jump to a label named ‘labelfive’.
Conclusion
We have come to the end of this tutorial on decision making constructs in C++. As already discussed, these decision-making constructs allow us to execute the code as per our choice.
In our upcoming tutorial, we will discuss loops in C++ in detail. Loops in C++ are used to execute a code repetitively. We will also discuss break and continue statements and their uses with examples.
=> Watch Out The Simple C++ Training Series Here.