Lecture 5&6 - Control Structures

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

CONTROL STRUCTURES

LEARNING OUTCOMES

• Apply basic control structure


• Write C/C++ program
Control Structures
• The essence of computer programming relies on telling the
computer what to do when something occurs, and how to do
it.
• Control Structures are used to control the flow of program's
execution.

– This is performed by setting conditions, examining them and


stating what decisions the computer should make.

– Help take charge of our programs.

– They serve to specify what has to be done by our program,


when and under which circumstances.
Control Structures
Block / sequence

• is a group of statements which are separated by


semicolons (;)
• One or more statements grouped together in a
block enclosed in braces:
{
statement1; statement2; statement3;
}
Control Structures
Cont.
1. If ……Else

if (score >= 50)


cout << “You Passed";
else
cout << “You Failed";
Control Structures
Cont.
2. While loop

void main (){


int num;
cout << "Enter the starting number > ";
cin >> num;
while (num > 0) {
cout << num << ", ";
num--;
}
Control Structures
Cont.
3. Do….While loop

int main (){


int num;
do {
cout << "Enter number (0 to end): ";
cin >> num;
cout << "You entered: " << num << "\n";
}
while (num != 0);
return 0;
}
Control Structures
Cont.
4. for loop

int main ()
{
for (int num = 10 ; num > 0; num --) {
cout << num << ", ";
}
cout << “Thank You!\n";
return 0;
}
Control Structures
Cont.
6. Selective Structure
switch (car) {
case 1: cout << “Pajero";
break;
case 2: cout << “Toyota";
break;
case 3: cout << “Mercedes";
break;
default:
cout << “Tuk Tuk";
}
Exercise Control Structures
Write a program whose output is shown
..
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)
Control Structures
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.
Control Structures
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.


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

}
Control Structures
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 3: 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 4: 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 5 : Displaying Numbers using do while

• 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 6: 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 7 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 8:Investment
Example 8: Computing Investment
#include<iostream> #include<iostream>
#include<cmath> #include<cmath>
#include<iomanip> #include<iomanip>
using namespace std; using namespace std;
int main() int main()
{ {
double p=1000; double p=1000;
double r=0.05; double r=0.05;
double a; double a;
int n;
int n;

cout<<fixed<<setprecision(2);
cout<<"Year”<<”\t\t”<<”Amount"<<endl;

for( n=1; n<=10; n++) cout<<"Year"<<setw(20)<<"Amount"<<endl;


{ for( n=1; n<=10; n++)
a=p*pow(1+r,n); {
cout<<n <<”\t\t”<<a<<endl; a=p*pow(1+r,n);
} cout<<n <<setw(25)<<a<<endl;
system("pause"); }
system("pause");
return 0;
} 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..
Cont..
• Exercises
1. Write a program to sum the even
numbers from 2 to 100.
2. Write a program to find out the largest of
three numbers using nested if—else
statement

You might also like