2. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
2
Types of Control Structure
•Sequence Structure
•Selection Structure
•Repetition Structure
int x;
x++;
Cout<<x;if
if/else
switch
For
While
do while
3. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
3
Selection Structure
If (condition)
Statement;
If/else
Syntax Example
If (x>y && x>z)
cout<<x;
If (x>y)
{
t=x;
x=y;
y=t;
}
if
If (condition)
statement1;
else
statment2;
If (n>0)
m=n;
else
m=-n;
If (x%2==0)
cout<<x<<“is Even”;
else
cout<<x<<“is Odd”;
swap
4. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
4
Selection Structure- Example-1
Finding the largest number among three numbers
Enter the numbers
12
20
8
The Largest number is 20
#include<iostream.h>
Int main()
{
int x,y,z,l;
cout<<“Enter the numbers/n”;
If(x>y)
l=x;
else
l=y;
If(z>l)
L=z;
cout<<“The Largest number is ”>>l;
Return 0;
}
Stop
Start
“Enter three Numbers”
Store Value In
X, Y, Z
Print L
IF X>Y L=X
L=Y
L=Z
Print L
IF Z>L
5. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
5
Selection Structure- Example-2
Converting Exam Percentage into Grade
#include<iostream.h>
Int main()
{
int p; char g;
cout<<“Enter Exam Percentage”;
cin>>p;
If(p>=90)
g=‘A’;
else if (p>=80)
g=‘B’;
else if (p>=70)
g=‘C’;
else
g=‘D’;
cout<<“the grade is”<<g;
Return 0;
}
Stop
Start
“Enter Exam Percentage”
Store Value In
p
IF p>=90 g=‘A’
g=‘B’
Print “the grade is” g
IF p>=80
IF p>=70 g=‘C’
g=‘D’
Enter the Percentage
the grade is C
#include<iostream.h>
Int main()
{
int p; char g;
cout<<“Enter Exam Percentage”;
cin>>p;
If(p>=90)
g=‘A’;
if (p>=80)
g=‘B’;
if (p>=70)
g=‘C’;
if (p<60)
g=‘D’;
cout<<“the grade is”<<g;
return 0;
}
6. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
6
Selection Structure- switch
Multiple selection.
Case a action
actionCase b
Case c action
Case z
Default
actions
break
break
break
action break
switch(Expression)
{
case constant1:
statement(s);break;
case constant2:
statement(s);break;
case constant3:
statement(s);break;
default:
statement(s);
}
7. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
7
Selection Structure- switch
#include<iostream.h>
#include<manip.h>
Int main()
{ float x,y; char op;
cout<<“Enter Operand Operator and another operand”;
cin>>x>>op>>y;
switch(op)
{
case ‘+’:
r=x+y;break;
case ‘-’:
r=x-y;break;
case ‘*’:
r=x*y;break;
case ‘/’: if(y!=0.0) r=x/y;
else {cout<<“Division by zero not allowed n”; r=0.0;}break;
default: cout<<“Unknown Operator”<<op<<“/n”; r=0.0;
}
cout<<“The result=“<<setw(7)<<r;
return 0; }
Enter Operand Operator and another operand
12.3+16.4
The result= 28.7
8. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
8
Repetition Structure
while (condition)
{ statement (s) ; }
do/while
Syntax Example
fact=1; //factorial
while(n>1)
{
fact=fact*n;
n=n-1;
}
While
do
{statement (s); }
while (condition)
fact=1;
Do
{
fact=fact*n;
n=n-1;
}
while(n>1)
for
{ statement (s) ; }
9. االحصائيه والبحوث الدراسات معهد القاهرة جامعة
والبرمجيات المعلومات قواعد بحوث مركز
9
e.g. investing example. Compute & print the amount invested in
n’th years for each year , for each year the amount(1+rate)^
number of years
#include<iostream.h>
#include<math.h>
#include <iomanip.h>
Int main()
{
double amount,p,r;
cout<<“Enter the origional amount invested”;
cin>>p;
cout<<“Enter the annual rate”;
cin>>r;
cout<<“Year”<<setw(21)<<“amount on deposit<<end;
for(int year=1;year<=10;year++)
{
amoun=p*pow(1+r, year);
cout<<year<<setw(21)<<setprecision(2)<<amount<<endl;
}
cin.get();
return 0;
}
set it to display in (21) right aligned
columns using setw(21)
Returns base raised to the power exponen
double
pow ( double base, double exponent )
- ios::left and ios::right, control
and right justification in your
output.
- precision, used to format floa
point
Set format flags