There come many situations when the programmer has to use C++ Conditional Statements to make decisions or has to execute statements based on some conditions. I this case, the For example, we need to process further only if there is a valid input by the user. This process is called decision making.
Decision making structures require the programmer to specify conditions to be tested or evaluated along with the statements to be executed in case the conditions are true.
Optionally the programmer can also write some other statements to be executed when the conditions are false. For example, we may also prompt for re-input if the input is not valid as exemplified formerly. The general form for evaluation of the statements of the decision making structures is.
There are two basic types of decision making structures; the if statements and switch statements. By extending these structures one can have five types which are given as
C++ Conditional Statements
- If statements
- If…else statements
- If…else if…else statements
- Nested if…else statements
- Switch statements
- Nested switch statements
The description of the C++ Conditional Statements is given in the following table.
Statement | Description |
if statement | In if statement condition/conditions are tested by a Boolean expression and one or more statements to be executed are followed |
if…else statement | In if… else statement condition/conditions are tested by a Boolean expression and one or more statements to be executed if the conditions are true else some other statements are executed i.e. when conditions are false |
if…else if…else statement | In if…else if…else statements, multiple conditions are tested. Among all, the true condition is executed and the result is displayed to the user. |
nested if statements | When if statements are used within the block of if statements then it is called nested if statements. |
switch statement | In a switch statement, a variable is tested for equality against a list of values. |
nested switch statements | When switch statements are placed within the block of switch statements then it is called nested switch statements. |
The details of the C++ Conditional Statements are present in the next sections.
C++ if statement
The if statement executes the code present in curly braces {} if the condition is true. If the condition is false then it bypasses the code present in curly braces. The syntax for the if statement is as follows:
if (condition) { //piece of code }
The below code demonstrates the working of if statement. The code asks user to input a value greater than 10. If value is greater than 10, it displayed the number entered y the user. But if the value is less than 10, the code in the if statement is not executed.
// This code is written by www.tutorialart.com #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer greater than 10" << endl; cin >> num; if (num > 10) { cout << "The number you entered is: " << num << endl; } cout << "End of program" << endl; return 0; }
In this case, user inputs the value 20.
Enter an integer greater than 10 20 The number you entered is: 20 End of program
For instance, user enters 5, a value smaller than 10. The output of the same code will be:
Enter an integer greater than 10 5 End of program
C++ if…else statement
The if else statement executes the code present in curly braces {} if the condition is true. If the condition is false, it executes the code present in curly braces {} after else keyword. The syntax for the if else statement is as follows:
if (condition) { //piece of code } else { //piece of code }
The below code demonstrates the working of if else statement:
// This code is written by www.tutorialart.com #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer greater than 10" << endl; cin >> num; if (num > 10) { cout << "The number you entered is greater than 10: " << num << endl; } else { cout << "The number you entered is not greater than 10: " << num << endl; } cout << "End of program" << endl; return 0; }
The output of the above code depends on the input of user. For example, if user enters a value greater than 10, the output will be:
Enter an integer greater than 10 13 The number you entered is greater than 10: 13 End of program
On the contrary, if user enters a value smaller than 10, output of the same code will be:
Enter an integer greater than 10 8 The number you entered is not greater than 10: 8 End of program
C++ Nested if…else statement
The if…elseif…else statement first validates the condition present in if statement. If its true, it executes the code present in curly braces and bypasses the rest of code. It its false, it jumps to the else if condition and check if the else if condition is true or not. It is true, it executes the code present in its curly braces. If its false, it jumps to the last and final statement else. The program executes the code present in curly braces of else. The syntax for the if…else if…else statement is as follows:
if (condition) { //piece of code } else if (condition) { //piece of code } else { //piece of code }
The code below demonstrates the working of if…elseif…else statement:
// This code is written by www.tutorialart.com #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer greater than 10" << endl; cin >> num; if (num > 10) { cout << "The number you entered is greater than 10: " << num << endl; } else if (num < 10) { cout << "The number you entered is smaller than 10: " << num << endl; } else { cout << "The number you entered is equal to 10: " << num << endl; } cout << "End of program" << endl; return 0; }
The above code can have 3 different outputs depending on user’s input.
For instance, if user enters 15, the output of the above code will be:
Enter an integer greater than 10 15 The number you entered is greater than 10: 15 End of program
If user enters value 5, the output will be:
Enter an integer greater than 10 5 The number you entered is smaller than 10: 5 End of program
If user enter 10 as input, the output will be:
Enter an integer greater than 10 10 The number you entered is equal to 10: 10 End of program
Nested if statement
Nested if statements have another if statement within an if statement. If the condition in the first if statement is true, then the second if statement is executed. But if the first if condition is false, the second if statement is not executed. The syntax for the nested if statement is as follows:
if (condition) { if (condition) { //piece of code } } else { //piece of code }
For example, here is a code that checks whether the number entered by the user is greater than 10 or not. There are two conditions if a number is not equal to 10. It’s either smaller than or greater than 10. In the below example the code first checks if the number entered by the user is equal to 10 or not. If the number is not equal to 10, it further checks if it is smaller than or greater than 10 and displays the respective result. If the input is equal to 10, it simply bypasses the nested if statements as the first if statement becomes false. The below example explains how nested if statements work:
// This code is written by www.tutorialart.com #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer greater than 10" << endl; cin >> num; if (num != 10) { if (num < 10) { cout << "The number you entered is smaller than 10: " << num << endl; } else { cout << "The number you entered is greater than 10: " << num << endl; } } else { cout << "The number you entered is equal to 10: " << num << endl; } cout << "End of program" << endl; return 0; }
The output of the above code when user enters 5 is:
Enter an integer greater than 10 5 The number you entered is smaller than 10: 5 End of program
The output of the code when user enters 10 is:
Enter an integer greater than 10 10 The number you entered is equal to 10: 10 End of program
When user enters value 15, the output of the code is:
Enter an integer greater than 10 15 The number you entered is greater than 10: 15 End of program
Switch Statement
Switch statement is also type of C++ Conditional Statements that changes the flow of program execution. When we have multiple possible cases to handle, we use switch statements. User can add multiple cases under the switch statement. Among all the cases, only one case’s value matches the value of the expression. The code associated to the case whose value equals the expression is executed. The syntax for the switch statement is as follows:
switch (variable / expression) { case constant1 : statement 1; break; case constant2 : statement2; break; . . default: statement3; break; }
The keyword break is optional. It is written after each case. The purpose of break keyword is to exit the switch block. Once the case value is matched and the program reaches its break keyword, the rest of the cases are not checked.
Similarly, the default keyword is optional too. In the scenario where none of the cases’ value matches the value of expression, the default statement is executed. There is no need to add break keyword after default.
The below example shows the working of switch statements. This program assigns grades on the basis of scores.
// This code is written by www.tutorialart.com #include <iostream> using namespace std; int main() { int grade = 80; switch (grade) { case 90: cout << "Your grade is A+"; break; case 80: cout << "Your grade is A"; break; case 70: cout << "Your grade is B+"; break; case 60: cout << "Your grade is B"; break; default: cout << "Your work hasn't been graded yet"; } return 0; }
Your grade is A
The same purpose can be achieved through repetitive if else statements. Instead of using a number of if…elseif…else statements, programmer can use switch statement.
Nested Switch Statement
Switch statement within a switch statement is nested switch statement. The second switch statement is present in one of the cases of the first switch statement. The program executes the second switch block only when the case containing it has a value equal to that of the variable. The syntax for the nested switch statement is as follows:
switch (variable1 / expression1) { case constant1: switch(variable2 / expression2) { case constant1: //piece of code break; case constant2: //piece of code break; default: } break; case constant2: //piece of code break; case constant3: //piece of code break; default: //piece of code }
This code gives grades according to the scores and subjects. If a student has 90 score in subject 1 then the grade would be different than the student having 90 score in subject 2. The example below shows the working of nested switch statements:
// This code is written by www.tutorialart.com #include <iostream> using namespace std; int main() { int grade = 90, subject = 2; switch (grade) { case 90: switch (subject) { case 1: cout << "Your grade is A+"; break; case 2: cout << "Your grade is A"; break; } break; case 80: cout << "Your grade is B+"; break; case 70: cout << "Your grade is B"; break; case 60: cout << "Your grade is B-"; break; default: cout << "Your work hasn't been graded yet"; } return 0; }
Your grade is A