0% found this document useful (0 votes)
2 views6 pages

Control Strcutures in C++

The document provides an overview of control structures in C++, which are essential for determining the order of code execution. It categorizes control structures into sequential, conditional, and looping types, detailing examples for each, including if statements, switch statements, and various loop types. Each control structure is explained with sample code to illustrate its usage in programming.

Uploaded by

nutrireadsurppot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Control Strcutures in C++

The document provides an overview of control structures in C++, which are essential for determining the order of code execution. It categorizes control structures into sequential, conditional, and looping types, detailing examples for each, including if statements, switch statements, and various loop types. Each control structure is explained with sample code to illustrate its usage in programming.

Uploaded by

nutrireadsurppot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Control Structures 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

Sequential control structure

 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;
}

Conditional control structures

 Enables execution of code statements based on the outcome of a certain condition


introduced somewhere within the code

 C++ has the following conditional control structures:

 If statement- The statements are executed if the condition is true. If (condition){


statements to be executed }.
 If -- else statement-. Some statements are executed if the conditions is true but if
the condition is false, some other statements are executed. If (condition)
{statements} else {statements}
 If---else if statement-. The control structure enable evaluation of multiple
conditions. The set of statements to be executed depends on which condition is
true. If (condition 1){statements set 1} else if(condition2){statements set 2}else
if(condition ..n) {statement set n}
 Switch statement-provides multiple alternative blocks of code to be executed just
like if else if statement

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;
}

Switch control structure


 Similar to if—else if structure in that multiple code segments are provided but only
one segment is executed depending on the condition provided.
 A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each
case

#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.

Looping Control Structure

 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 (initial counter value; condition; counter increment) {


// code block to be executed repeatedly
}

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;
}

 While loop-A condition is set and tested as the execution starts.


The block of code within the loop is executed multiple times long as the
condition is true.
The looping stops when the condition evaluates to false.
while (condition) {
// code block to be executed repeatedly
}

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

You might also like