C++ Control Structures and Programs
C++ Control Structures and Programs
1. Sequence Constructs
Instructions are executed in the order in which they appear from the first line
to the last . All instructions are executed
2. Selection Constructs
Brings reasoning and decision making in programming
Instructions are executed if a condition is met. Not all instructions are
executed.
Examples of selection constructs
a. if construct
b. if…else construct
c. if…else if… else construct
d. switch construct
a. if construct
it tests a condition and execute some code if the condition is met
Syntax
if (condition) {
code to execute if the condition is met.
}
b. if…else construct
it tests a condition and execute some if the condition is met. It also provides an
alternative set of code to execute if the condition is not met.
Syntax
if (condition) {
code to execute if the condition is met.
} else {
Alternative code to execute if the condition is NOT met.
}
Example
/*
Checking if the first number is
divisible by the second number
*/
#include<iostream>
using namespace std;
int main () {
// variable declaration
int num1;
int num2;
int rem;
//get input from the user
cout<<"Enter a number";
cin>>num1;
cout<<"Enter the second number";
cin>>num2;
// perform a modulo operation
rem = num1 % num2;
// determine or check divisibility
if (rem == 0) {
cout<<"First number is divisible by the second number"<<endl;
} else {
cout<<"First number is NOT divisible by the second number"<<"\n";
}
return 0;
} // closing main function
Example
/* A program that accepts marks for 3 units
and then calculates the total and average marks.
The program then grades a student based on the
following average mark criteria
80-100 A, 70-79 B, 60-69 C, 50-59 D, 0-49 Fail
Anything else is invalid
*/
#include<iostream>
using namespace std;
int main () {
float Maths;
float English;
float Kiswahili;
float Total;
float Average;
return 0;
}
d. Switch construct
Evaluates several expressions (conditions) and provides codes to execute if the expression
matches a given criteria. It also provides an alternative set of code to execute if no expressions
gets a match.
Syntax
switch (expression) {
case expression1;
code to execute if the expression matches expression1
break;
case expression2;
code to execute if the expression matches expression2
break;
default:
code to execute if no expression is matched
}
3. Looping
A loop is used to performs a task repeatedly as long as a condition is met.
A loop executes some code repeatedly as long as a condition is met.
Types of Loops
a. do…while loop
b. while loop
c. for loop
a. do…while loop
It is a post condition loop because it checks the condition after executing its code. It executes
its code at least once even if the condition is false.
Syntax
do {
code to execute
increment/decrement operation
} while (condition);
The following program generates a list of 9 numbers in the order 3,5,7 and then find their sum.
b. while loop
It is a precondition loop because it evaluates the condition before executing its code. This
means that if the condition is false, its code will never be executed.
It will execute its code as long as the condition is true.
Syntax
while (condition) {
code to execute
increment/decrement operation
}
The following program generates a list of 12 numbers in the order 3,12,48 and then find their
sum.
while (count<=12) {
cout<<num<<endl; //print the series value
sum = sum + num; // perform a sum operation
num = num * 4; // multiply num by 4
count = count + 1; // increment operation to
//control the loop's condition
}
c. for loop
The for loop executes its code for a specific number of times as specified within the
condition.
Syntax
for (initialCountValue; condition; increment) {
code to execute
}
// Using the for loop
//The following program generates a list of
//7 numbers in the order 2,6,18
//and then find their sum.
#include<iostream>
using namespace std;
int main (){
int count; // will count the number 1-7
int num; // generated number
int sum;// to hold the sum of the 7 numbers
num = 2; // initial value of the generated numbers
sum = 0; // inital value of sum
//these variable values will keep changing in
// every loop
}
cout<<"The sum of all is " << sum;
return 0;
}