Chapter 3
Control Structure
Ragia A. Ibrahim, Ph.D. Student
1
‫القاهرة‬ ‫جامعة‬
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬
‫مركز‬‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
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
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
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
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
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
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
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
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
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
10
Nested loops
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
11
Example1:
#include<iostream.h>
Int main()
{ int linenumber, i ;
for(linenumber=1; linenumber<=5;linenumber++)
{
for(i=1;i<=linenumber;i++)
cout<<i;
//when:
cout<<endl;
}
cin.get();
return 0;
}
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
12
#include<iostream.h>
Int main()
{ int linenumber, i ;
for(linenumber=1; linenumber<=5;linenumber++)
{
for(i=1;i<=5-linenumber;i++)
cout<<‘ ‘;
for(j=1;j<=2*linenumber-1;j++)
cout<<‘*’;
cout<<endl;
}
cin.get();
return 0;
}
Example2:
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
13
#include<iostream.h>
Int main()
{ int linenumber, i ;
for(linenumber=1; linenumber<=5;linenumber++)
{
for(i=1;i<=linenumber;i++)
cout<<' ';
for(j=10;j> 2*linenumber-1;j--)
cout<<'*';
cout<<endl;
}
cin.get();
return 0;
Example3:
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
14
#include<iostream.h>
Int main()
{ int linenumber, i ;
for(linenumber=1; linenumber<=5;linenumber++)
{
for(i=1;i<=linenumber;i++)
cout<<' ';
for(j=10;j> 2*linenumber-1;j--)
cout<<'*';
cout<<endl;
}
cin.get();
return 0;
Example3:

Chapter 3

  • 1.
    Chapter 3 Control Structure RagiaA. Ibrahim, Ph.D. Student 1 ‫القاهرة‬ ‫جامعة‬ ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫مركز‬‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬
  • 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
  • 10.
    ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 10 Nested loops
  • 11.
    ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 11 Example1: #include<iostream.h> Int main() { int linenumber, i ; for(linenumber=1; linenumber<=5;linenumber++) { for(i=1;i<=linenumber;i++) cout<<i; //when: cout<<endl; } cin.get(); return 0; }
  • 12.
    ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 12 #include<iostream.h> Int main() { int linenumber, i ; for(linenumber=1; linenumber<=5;linenumber++) { for(i=1;i<=5-linenumber;i++) cout<<‘ ‘; for(j=1;j<=2*linenumber-1;j++) cout<<‘*’; cout<<endl; } cin.get(); return 0; } Example2:
  • 13.
    ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 13 #include<iostream.h> Int main() { int linenumber, i ; for(linenumber=1; linenumber<=5;linenumber++) { for(i=1;i<=linenumber;i++) cout<<' '; for(j=10;j> 2*linenumber-1;j--) cout<<'*'; cout<<endl; } cin.get(); return 0; Example3:
  • 14.
    ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 14 #include<iostream.h> Int main() { int linenumber, i ; for(linenumber=1; linenumber<=5;linenumber++) { for(i=1;i<=linenumber;i++) cout<<' '; for(j=10;j> 2*linenumber-1;j--) cout<<'*'; cout<<endl; } cin.get(); return 0; Example3: