Control Strcutures in C++
Control Strcutures in C++
Control structures are predefined statements that are used when writing a program to
indicate the sequence or order in which the code statements should be executed
There are 3 main classifications of control structures
i. Sequential
ii. Conditional/selection control structures
iii. Looping/ repetition control structures
Unless directed otherwise, C++ statements execute one after the other in the order in
which they are written—that is, in sequence
In the example below , all the statements will be executed one after the other
sequentially.
#include <iostream>
using namespace std;
int main(){
int x=8, y=4,z=3;
int sum;
sum=x+y+z;
cout<< “the total is:”<<sum;
return 0;
}
If statement
Use the if statement to specify a block of C++ code to be executed if a condition is true.
1
Prepared by Joel Gichuhi
if (condition) {
// block of code to be executed if the condition is true
}
for example
#include <iostream>
using namespace std;
int main()
{
int age;
cout<< “enter age”<<endl;
cin>>age;
if (age > 18)
{
cout << "Adult";
}
return 0;
}
If—else statement
In if---- else control structure statements specified in the else { } code section are
executed if the condition is false.
Example:
#include <iostream>
using namespace std;
int main(){
int age;
cout<< “enter age”<<endl;
cin>>age;
if (age > 18) {
cout << "Adult.";
} else {
cout << "Under age.";
}
return 0;
}
If ..else if statement
Use the if--else if control structure when a program has more than 2 possible outcomes
depending on the provided conditions.
Example:
#include <iostream>
using namespace std;
int main(){
2
Prepared by Joel Gichuhi
int marks;
cout<< “enter marks”<<endl;
cin>>marks;
if (marks >=80) {
cout << "Distinction.";
} else if (marks >= 60) {
cout << "Credit.";
} else if(marks >= 50) {
cout << "Pass.";
}else if(marks <50) {
cout << "Fail.";
}
else{
cout << " the marks entered is invalid";
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
int day;
cout<< “enter a number between 1 and 7”<<endl;
cin>>day;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
3
Prepared by Joel Gichuhi
case 7:
cout << "Sunday";
break;
default:
cout << "Day numbers must be between 1-7";
}
return 0;
}
The default case can be used for performing a task when none of the cases is true.
No break is needed in the default case.
Allow some statements to be executed multiple times until a certain condition is met.
Looping control structures save time, reduce errors, and they make code more
readable.
Looping control structure in C++ include:
For loop- used when you know exactly how many times you want a block of code
to be executed repeatedly.
Syntax
for example
#include <iostream>
using namespace std;
int main(){
for (int m = 0; m <5; m++) {
cout << “I love coding << "\n";//this line of code will be executed 5 times
}
return 0;
}
4
Prepared by Joel Gichuhi
for example
//
#include <iostream>
using namespace std;
int main(){
int m = 0;
while (m<5) {
cout << “I love coding” << endl;
m++;
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
int k = 20;//k is the variable that keep track of the looping
while (k > 10) {// condition given here
cout << This is a while loop << endl;//statement to be looped
k--;// decrement variable k by 1on each loop.
}
return 0;
}
Do while loop
The do/while loop is a similar to the while loop but the condition is stated
after the block of statements to be looped.
Thus the statements will be executed at least once before the condition is
evaluated.
The looping stops when the condition becomes false.
Syntax:
do {
// code block to be executed
}
while (condition);
For example
#include <iostream>
using namespace std;
int main(){
int k = 1;//k is the variable that keep track of the looping
do{
cout << This is a do while loop << endl;//statement to be looped
k++;// increment variable k by 1.
5
Prepared by Joel Gichuhi
while (k < 10) {// condition given after the block of code to be looped
}
return 0;
}
6
Prepared by Joel Gichuhi