Week 3 & Week 4 Control Structures

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Control Structures

Prepared By Dalton Ndirangu


Control Structures
Control structures used in C++

 If (is a single selection control structure)


 if/else loop (is a double selection control structure)
 while loop (is a repetitive conditional control structure)
 do/while loop (is a repetitive conditional control structure)
 for loop (is unconditional control structure)
 switch loop (is multiple selection control structure)
if/else control structures

 These are conditional selection control structures. They allow selection to be


made based on two possible outcomes. Select is performed if the condition is
true or skipped and perform on else statements if the condition is false.
The general format of if/else:

 if(condition)
 {
 Action1;
 }
 else
 {
 Action2;
 }
 Note: If condition is satisfied, only action1 is executed else action2 is executed.
In all instances, the use of action stands for one or many valid statements. The
condition is an expression. An expression is a single value or an algebraic
statement.
Example 5.1: Computing Largest of three
numbers
Algorithm: Computing largest of three numbers

1. Request for three numbers


2. Input three numbers say a, b, c
3. Assume a is the largest
4. If b>large then b is the largest else c is the largest
5. Display the largest
#include<iostream>
using namespace std;
int main()
{
int a,b,c, large;
cout<<"Enter three numbers";
cin>>a>>b>>c;

large=a;

if(b>large)
large=b;
else
large=c;

cout<<" The largest is "<<large;


system("pause");

return 0;
}
Example 5.2: Evaluating Even and Odd
Numbers
Algorithm:Evaluating Even and Odd numbers
1. Request/Prompt for a number
2. Input the number
3. If the remainder of the number divided by 2 is equal to
zero
3.1 display message number is even

3.2 if remainder is not equal to zero, the number is


odd

Solution
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number";
cin>>n;

if(n%2==0)
cout<<" The Number " <<n << " is Even";
else
cout<<" The Number "<< n << " is Odd";

system("pause");

return 0;

}
While Control Structure

 This is a repetitive conditional control structure. The loop is tested at the


beginning. A program repeats an action while some condition remains true.
 The general format is:
Example 6: Displaying using while loop
Algorithm:Displaying numbers from 1 to 20

1. Initialize number counter to 1


2. Set while loop to 20
3. While loop is valid
i. Display the number
ii. Increment counter number by 1
4. End

Solution
#include<iostream>
using namespace std;
int main()
{
int n=1;
while(n<=20)
{
cout<<n<<"\n";
n=n+1;
}
system("pause");

return 0;
}
Example 7.1: Displaying Numbers Squares
and Cubes from 1 to 20

#include<iostream>
using namespace std;
int main()
{
int n=1;
cout<<"Number\tSquare\tCube"<<endl;
while(n<=20)
{
cout<<n<<n << "\t" << n*n <<"\t" <<n*n*n <<endl;

n++;
}
system("pause");

return 0;
}
Do while Control Structure

 This is a repetitive conditional control structure. The loop is tested at the


end.
 The general format is:
 do
 {
 Action;
 }while (condition);
Example 7.2 : Displaying Numbers using do
while

#include<iostream>
using namespace std;
int main()
{
int n=1;
cout<<"Number\tSquare\tCube"<<endl;
do
{
cout<<n<<n << "\t" << n*n <<"\t" <<n*n*n <<endl;
n++;

}while(n<=20);
system("pause");

return 0;
}
For Control Structure

 This is unconditional control structure. The loop is automatically incremented

 The general format is:

 for (initialvalue; testcondition; incrementalvalue)

 {

 Action;

 }
Cont..
Example 8.1: Displaying numbers using
for loop
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Number\tSquare\tCube"<<endl;
for(n=1; n<=20; n++)
cout<<n <<"\t" <<n*n <<"\t" <<n*n*n <<endl;
system("pause");

return 0;
}
Example 8.2 Displaying with setwidth
Manipulation
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int n;
cout<<"Number"<< setw(15) <<"Square"<< setw(15) <<"Cube"<<endl;
for(n=1; n<=20; n++)
cout<<n <<setw(15) <<n*n <<setw(15) <<n*n*n <<endl;
system("pause");

return 0;
}
Example:Investment
Example 9.1: Computing Investment
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double p=1000;
double r=0.05;
double a;
int n;

cout<<"Year”<<”\t\t”<<”Amount"<<endl;

for( n=1; n<=10; n++)


{
a=p*pow(1+r,n);
cout<<n <<”\t\t”<<a<<endl;
}
system("pause");

return 0;
}
Example 9.2: Computing Investment
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double p=1000;
double r=0.05;
double a;
int n;

cout<<fixed<<setprecision(2);

cout<<"Year"<<setw(20)<<"Amount"<<endl;
for( n=1; n<=10; n++)
{
a=p*pow(1+r,n);
cout<<n <<setw(25)<<a<<endl;
}
system("pause");

return 0;
}
Switch Control Structure
 This is a conditional multiple selection control structure. It allows several cases to be executed.
 The general format is:
 switch (value)
 {
 case 1:
 {
 Action1;
 break;
 }
 case 2:
 {
 Action2;
 break;
 }
 default:
 {
 Action3;

 }
 }
 Note that the command break enables one to come out of a loop
Cont..

You might also like