The control structure is one of the major concepts in all programming today. In a previous article, we look at the basic concept of control structures in all languages but in this two-part series, we will look at all the different types of control structures used in C++. With the control structure in C++, there are two main statements which are the Selection statements and Iteration statements. In part 1, we will look at selection statements.
Selection if..else statement in the control structure
The general form of an if..else statement is
where if the result of the expression is true, the first statement is executed, otherwise, the second statement is executed.
In the example below, a simple if else statement is shown below, where if the user enters in a number greater or equal to 10, it will output “The number you have entered is greater than 10”. If the user entered in a number less than 10, it will show “The number you have entered is less than 10”.
#include <iostream> using namespace std; int main() { // Variable to get a number from the user int number; cout << "Enter in a number: "; // Get the user to enter in a number cin >> number; // When the number is greater or equal to 10 if(number >= 10) cout << "The number you have entered in is greater than 10"; // When the number is less than 10 else cout << "The number you have entered in is less than 10"; }
Nested if..else in the control structure
A nested if else statement deals with a more complex control statement, but it can be easily understood visually with the diagram below.
The example nested if..else statement code being applied here is a program where you enter in your age to find out what your risk of getting into an accident by driving. If you are 21 or under, the program will check to see if you are either 18 or below. It will then show your risk. If your age is greater than 21, it will check to see if your age is equal or less than 23, and then display your risk.
#include <iostream> using namespace std; int main() { // Store the age input here int age; cout << "Enter in your age to find out what of risk of driving a car isn"; // Enter in age cin >> age; // The age is less or equal to 21 if(age <= 21) { // Age is less than or equal to 18 if(age <= 18) { cout << "Very high risk"; } else { cout << "High risk"; } } else { if(age <= 23) { cout << "Medium Risk"; } else { cout << "Low Risk"; } } }
Multi-way decisions if else statement
Multi-way decisions are different from nested if..else statements because instead of creating another inner if..else statement, it just uses “else if” in the next line.
The sample multi-way decision code is a program to calculate university grades by getting the user to enter in their final grade mark. When the user has entered their grade, the multi-way statement will compute the grade the user has achieved.
#include <iostream> using namespace std; int main() { // Store the mark entered in int mark; cout << "University grade calculator\n”; cout << "Enter in your mark to see your grade\n”; // Enter in the student mark cin >> mark; // The Multiway statement will then give the grade // To the screen if(mark >= 80) { cout << "High Distinction\n”; } else if(mark >= 70) { cout << "Distinction\n”; } else if(mark >= 60) { cout << "Credit\n”; } else if(mark >= 50) { cout << "Pass\n”; } else { cout << "Fail\n”; } }
Switch Statement
A switch statement works exactly like the multi-way decision but in C++, it is known to be more efficient than if..else statements. The switch expression must evaluate to either a character or a constant integer value. Integer values with a specific range are not supported in switch statements. When dealing with larger programs, switch statements have the advantage because the value of the expression is successively tested with a list of constants. Those constants are known as the “case” in the code. When a match is found in the switch statement, the specific line of code is executed in the program.
The switch statement code sample is a program where the user enters a number between 1 – 12. Once the number has been inputted, the program will then take the number inputted and match it to a case in the switch statement, and then display the month.
/* Switch statement Written By Sahil Bora */ #include <iostream> using namespace std; int main() { // The variable used to look up the month int month; cout << "Enter in a number between 1 − 12 and the month will\n"; cout << "Show up\n"; cin >> month; switch(month) { case 1: cout << "January"; break; case 2: cout << "February"; break; case 3: cout << "March"; break; case 4: cout << "April"; break; case 5: cout << "May"; break; case 6: cout << "June"; break; case 7: cout << "July"; break; case 8: cout << "August"; break; case 9: cout << "September"; break; case 10: cout << "October"; break; case 11: cout << "November"; break; case 12: cout << "December"; break; } }