#9 Control Structures, If Else and Switch-Case Statement in C++
We will visualize the control structure, if-else, and switch statements in the C++ language in this lecture. In our last lesson, we discussed the constant, manipulators and operator precedence in C++.
Last updated
the topics which we are going to cover today are given below:
Control Structures in C++
IF Else in C++
Switch Statement in C++
Control Structures in C++
The work of control structures is to give flow and logic to a program. There are three types of basic control structures in C++.
Sequence Structure
Sequence structure refers to the sequence in which program execute instructions one after another. An example diagram for the sequence structure is shown in figure 1.
Figure 1: Sequence Structure
Selection Structure
Selection structure refers to the execution of instruction according to the selected condition, which can be either true or false. There are two ways to implement selection structures, by “if-else statements” or by “switch case statements”. An example diagram for selection structure is shown in figure 2.
Figure 2: Selection Structure
Loop Structure
Loop structure refers to the execution of an instruction in a loop until the condition gets false. An example diagram for loop structure is shown in figure 3.
Figure 3: Loop Structure
If Else Statements in C++
As we have discussed the concepts of the different control structure, If else statements are used to implement a selection structure. An example program for if-else is shown in figure 4.
Figure 4: If-Else program in C++
As shown in figure 4, we declared a variable “age” and used “cin" function to gets its value from the user at run time. At line 10 we have used "if” statement and give a condition “(age<18)” which means that if the age entered by the user is smaller than "18” the output will be “you cannot come to my party” but if the age is not smaller than “18” the compiler will move to the next condition.
At line 13 we have used “else if” statement and given another condition “age==18" which means that if the age entered by the user is equal to "18” the output will be “you are a kid and you will get a kid pass to the party” but if the age is not equal to the “18” the compiler will move to the next condition.
At line 16 we have used “else" condition which means that if none of the above condition is "true" the output will be "you can come to the party”.
The output for the following program is shown in figure 5.
Figure 5: If-Else Program Output
As can be seen in figure 5, that when we entered the age "81" which was greater than 18, so it gives us the output "you can come to the party”. The main thing to note here is that we can use as many “else if” statements as we want.
Switch Case Statements in C++
In switch-case statements, the value of the variable is tested with all the cases. An example program for the switch case statement is shown in figure 6.
Figure 6: Switch Case Statement Program
As shown in figure 4, we passed a variable “age” to the switch statement. The switch statement will compare the value of variable “age" with all cases. For example, if the entered value for variable "age” is “18”, the case with value “18” will be executed and prints “you are 18”. The keyword “break" will let the compiler skips all other cases and goes out of the switch case statement. An output of the following program is shown in figure 6.
Figure 7: Switch Case Statement Program Output
As shown in figure 7, we entered the value “18” for the variable “age", and it gives us an output "you are 18” and “Done with switch case”. The main thing to note here is that after running the “case 18” is skips all the other cases due to the “break” statement and printed “Done with switch case” which was outside of the switch case statement.
Code as described/written in the video
#include<iostream>usingnamespace std;intmain(){ // cout<<"This is Module 9";int age; cout<<"Tell me your age"<<endl; cin>>age; // 1. Selection control structure: If else-if else ladder // if((age<18) && (age>0)){ // cout<<"You can not come to my party"<<endl; // } // else if(age==18){ // cout<<"You are a kid and you will get a kid pass to the party"<<endl; // } // else if(age<1){ // cout<<"You are not yet born"<<endl; // } // else{ // cout<<"You can come to the party"<<endl; // } // 2. Selection control structure: Switch Case statementsswitch (age) {case18: cout<<"You are 18"<<endl;break;case22: cout<<"You are 22"<<endl;break;case2: cout<<"You are 2"<<endl;break;default: cout<<"No special cases"<<endl;break; } cout<<"Done with switch case";return0;}