Control Structures: Aryaf Aladwan
Control Structures: Aryaf Aladwan
Control Structures: Aryaf Aladwan
Control Structures
1
Aryaf Aladwan
Control Structures
• 3 control structures:
– Sequence structure
• Programs executed sequentially by default
– Selection structures
• if, if/else, switch
– Repetition structures
• while, do/while, for
2
Aryaf Aladwan
C++ keywords
Cannot be used as identifiers or variable names
C++ Keywords
• Selection structure
– Choose among alternative courses of action
– Pseudocode example:
If student’s grade is greater than or equal to 50
Print "Passed"
– If the condition is true
• Print statement executed, program continues to next statement
– If the condition is false
• Print statement ignored, program continues
– Indenting makes programs easier to read
• C++ ignores whitespace characters (tabs, spaces, etc.)
4
Aryaf Aladwan
5
Aryaf Aladwan
6
Aryaf Aladwan
7
Aryaf Aladwan
8
Aryaf Aladwan
9
Aryaf Aladwan
10
Aryaf Aladwan
11
Aryaf Aladwan
12
Aryaf Aladwan
13
Aryaf Aladwan
14
Aryaf Aladwan
15
Aryaf Aladwan
16
Aryaf Aladwan
17
Aryaf Aladwan
18
Aryaf Aladwan
19
Aryaf Aladwan
20
Aryaf Aladwan
21
Aryaf Aladwan
if Selection Structure
• Syntax of if - else:
if ( condition)
statement;
else
statement;
• Translation into C++
If student’s grade is greater than or equal to 50
Print "Passed"
if ( grade >= 50 )
cout << "Passed";
• Diamond symbol (decision symbol)
– Indicates decision is to be made
– Contains an expression that can be true or false
• Test condition, follow path
22
Aryaf Aladwan
if Selection Structure
23
Aryaf Aladwan
if/else Selection Structure
• if
– Performs action if condition true
• if/else
– Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 50
print "Passed"
else
print "Failed"
• C++ code
if ( grade >= 50 )
cout << "Passed";
else
cout << "Failed";
24
Aryaf Aladwan
Ternary conditional operator (?:)
false true
grade >= 50
25
Aryaf Aladwan
Example1
• Write a program to test if a number is positive or negative
#include<iostream.h> Start
void main()
{
int x
int x;
cout<<"Enter any number: \n";
cin>>x; Input x
if(x>=0)
{ T
F
cout<<"positive number"; If ( x>=0)
}
else
{ negative positive
cout<<"negative number";
}
}
End
OUTPUT Enter any number:
-8
26 negative number
Aryaf Aladwan
Example2
• Write a program to test if a month entered from keyboard is valid month or not?
#include<iostream.h>
void main()
{
int month;
cout<<"Enter the month:"<<endl;
cin>>month;
if(month>=1 && month<=12)
cout<<month<<" is a valid month"<<endl;
else
cout<<month<<" is not a valid month";
}
27
Aryaf Aladwan
Example3
• Write a program to test if a number is odd or even
#include<iostream.h>
void main()
{
int x;
cout<<"Enter any number: \n";
cin>>x;
if(x%2==0)
{
cout<<"even number";
}
else
{
cout<<"odd number";
}
}
32
Aryaf Aladwan
nested if/else example
#include<iostream.h>
void main()
{
int grade;
cout<<"enter the grade"<<endl;
cin>>grade;
if ( grade >= 90 )
cout << "A";
else if ( grade >= 80 )
cout << "B";
else if ( grade >= 70 )
cout << "C";
else if ( grade >= 60 )
cout << "D";
else
cout << "F";
}
OUTPUT Enter the grade:
33 66
D Aryaf Aladwan
if/else Selection Structure
• Compound statement
– Set of statements within a pair of braces
if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
– Without braces,
cout << "You must take this course again.\n";
always executed for all grades
• Block
– Set of statements within braces
34
Aryaf Aladwan
Another example
x − 1, x a;
2
Z = x , a x b
2 x , x b
35
Aryaf Aladwan
The Flow Chart
Start
int x,z,a,b
Input x
F T
If x<= a
F T
If x<b
z=x-1
z= z * x z = x2
Print z
End
36
Aryaf Aladwan
#include<iostream.h>
void main()
{
int x,z,a,b;
cout<<“Enter a"<<endl;
cin>>a; OUTPUT
cout<<“Enter b \n"; Enter a:
cin>>b; 2
cout<<“Enter x"<<endl; Enter b:
cin>>x; 7
if (x<=a) Enter x:
z=x-1; 6
Z = 36
else if (x<b)
z=x*x;
else
z=2*x;
cout<<" z = "<<z;
}
37
Aryaf Aladwan
#include<iostream.h>
void main()
{
int x;
cout<<"enter x:"<<endl;
cin>>x; OUTPUT
if (x>50)
enter x: 77
{
pass
if (x>90)
cout<<"excellent";
else if (x>80) enter x: 93
cout<<"very good"; excellent
else
cout<<"pass"; enter x: 88
very good
}
else enter x: 44
cout<<"fail"; fail
}
38
Aryaf Aladwan
Switch Multiple-Selection Structure
- switch keyword
- Test variable for multiple values
- Series of case labels and optional default case
switch ( variable )
{
case value1 : statements; break; // taken if variable == value1
case value2 : statements; break; // taken if variable == value2
case value3 :
case value4 : statements; break; // taken if variable == value3 or value4
case value5 : statements; break; // taken if variable == value5
default : statements; break; // taken if variable matches no other cases
}
39
Aryaf Aladwan
40
Aryaf Aladwan
switch Multiple-Selection Structure
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
41
Aryaf Aladwan
Cont.
42
Aryaf Aladwan
Cont.
• Example upcoming
– Program to read grades (A-F)
– Display number of each grade entered
• Details about characters
– Single characters typically stored in a char data type
• char a 1-byte integer, so chars can be stored as ints
– Can treat character as int or char
• 97 is the numerical representation of lowercase ‘a’ (ASCII)
43
Aryaf Aladwan
#include <iostream.h>
void main()
{
char grade;
cout << "Enter the letter grades." << endl;
cin>> grade;
switch ( grade )
{
case 'A': cout<<"The grade is: A"; break;
case 'B': cout<<"The grade is: B"; break;
case 'C': cout<<"The grade is: C"; break;
case 'D': cout<<"The grade is: D"; break;
case 'F': cout<<"The grade is: F"; break;
default: cout << "Incorrect letter grade entered! Enter a new grade\n ";
break; // optional; will exit switch anyway
} // end switch
}
OUTPUT 1 OUTPUT 2
44
#include <iostream.h>
void main()
{
int y = 1;
switch (y)
{
case 1:cout<<"welcome"; break;
case 2:cout<<" to our lecture"; break;
case 3:break;
default :cout<<"bye\n";
}
}
OUTPUT 1
welcome
45
46
Aryaf Aladwan
47
Aryaf Aladwan
48
Aryaf Aladwan
49
Aryaf Aladwan
goto
• goto statement allows the programmer to transfer the control to another
destination in the program.
• Iteration structure replace the goto statement.
#include <iostream.h>
void main()
{
float x; OUTPUT 1
x=0;
0 0.2 0.4 0.6 0.8 1 1.2
L1: 1.4 1.6 1.8
cout<<x<<" ";
x=x+0.2;
if(x<=2.0)
goto L1;
}
50
Aryaf Aladwan
51
Aryaf Aladwan
52
Aryaf Aladwan
53
Aryaf Aladwan
54
Aryaf Aladwan
55
Aryaf Aladwan
56
Aryaf Aladwan
57
Aryaf Aladwan
58
Aryaf Aladwan
59
Aryaf Aladwan
60
Aryaf Aladwan
61
Aryaf Aladwan
62
Aryaf Aladwan
63
Aryaf Aladwan
64
Aryaf Aladwan
65
Aryaf Aladwan
66
Aryaf Aladwan
Iteration Structure
• For loop
• Nested For loop
• While loop
• Do … while loop
67
Aryaf Aladwan
68
Aryaf Aladwan
69
Aryaf Aladwan
70
Aryaf Aladwan
71
Aryaf Aladwan
72
Aryaf Aladwan
73
Aryaf Aladwan
74
Aryaf Aladwan
75
Aryaf Aladwan
76
Aryaf Aladwan
77
Aryaf Aladwan
78
Aryaf Aladwan
79
Aryaf Aladwan
80
Aryaf Aladwan
81
Aryaf Aladwan
82
Aryaf Aladwan
83
Aryaf Aladwan
84
Aryaf Aladwan
85
Aryaf Aladwan
86
Aryaf Aladwan
87
Aryaf Aladwan
88
Aryaf Aladwan
for Repetition Structure
• General format when using for loops
for ( initialization; condition; increment )
{
statement;
} initialization
T
condition statement increment
• Example
for( int counter = 1; counter <= 10; counter++ )
cout << counter << endl;
– Prints integers from 1 to 10 No semicolon after last
statement
89
Aryaf Aladwan
#include <iostream.h>
void main()
{
OUTPUT
counter = 1
1
2
3 T
4 counter <= 10 Print counter counter ++
5
6 F
7
8
9
10
90
for Repetition Structure
• Initialization and increment
– For multiple variables, use comma-separated lists
for (int i = 0, j = 0; j + i <= 10; j++, i++)
cout << j + i <<" ";
Output is : 0 2 4 6 8 10
- Ex:
- int x=1,y=2;
for (int j = x; j<= 4*x*y ; j+= y /x)
cout<<j<<" ";
Output is : 1 3 5 7
- Ex:
- for (int j = 2; j<= 20; j+=3)
cout<<j<<" ";
output is : 2 5 8 11 14 17 20
91
Aryaf Aladwan
92
Aryaf Aladwan
Example 1
OUTPUT
Hello World
Hello World
Hello World
Hello World
Hello World
93
Aryaf Aladwan
Example 2
Write a program to calculate the sum for even integers from 2 through 100.
#include <iostream.h>
void main()
{
int sum = 0;
// sum even integers from 2 through 100
for ( int number = 2; number <= 100; number += 2 )
sum += number;
cout << "Sum is " << sum << endl;
}
OUTPUT for the two methods
Sum is 2550
#include <iostream.h>
void main()
{
int sum = 0;
for ( int number = 1; number <= 100; number++)
{
if(number%2= =0)
sum += number;
}
cout << "Sum is " << sum << endl;
}
94
Example 3
• Write a program to characters from a to r.
#include <iostream.h>
void main()
{
char letter;
for (letter='a'; letter<='r'; letter++ )
{
cout << "\t" <<letter<<"\t";
}
}
OUTPUT
a b c d e
f g h i j
k l m n o
p q r
95
Aryaf Aladwan
Example 4
{
10 9 8 7 6 5 4 3 2 1
cout << n << " ";
}
}
96
Aryaf Aladwan
Example 5 Start
marks?
int i = 0
F
If ( i < n)
T
average = sum / n
Input grade
Print average
sum = sum + grade
End i++
97
Aryaf Aladwan
The code
#include <iostream.h>
OUTPUT
void main()
{
enter how many marks?
int grade ,n, sum=0;
5
float average;
enter marks
cout<<"enter how many marks?"<<endl;
77
cin>>n;
60
cout<<"enter marks"<<endl;
83
for ( int i=0;i<n; i++)
90
{
53
cin>>grade;
average is 72
sum= sum +grade;
}
average= sum / n;
cout<<"average is "<<average;
}
98
Aryaf Aladwan
Example 6
• Write a program to find the factorial of n?
#include <iostream.h>
void main()
{ n!= 1x2x3 ….. (n-1)xn
int k, fact=1; Fact k = fact k-1 x k
cout<<"enter k";
cin>>k;
if (k<0)
fact=0; OUTPUT
99
Aryaf Aladwan
100
Aryaf Aladwan
Example 7
• Write a program for determining any mathematical operator entered
from the keyboard?
#include <iostream.h>
void main()
{
char symbol;
cout << "Enter the mathematical operator (+ add, - sub , * mul , / for div , % for mod)" << endl;
for (int i=0;i<=5;i++)
{
cin>> symbol;
switch ( symbol )
{
case '+': cout<<"The operation is addition"; break;
case '-': cout<<"The operation is subtraction"; break;
case '*': cout<<"The operation is multiplication"; break;
case '/': cout<<"The operation is division"; break;
case '%': cout<<"The operation is modulus"; break;
default: cout << "Incorrect operator\n ";break;
}
}
} 101
Aryaf Aladwan
OUTPUT
Enter the mathematical operator (+ add, - sub , * mul , / for div , % for mod )
+
The operation is addition
-
The operation is subtraction
*
The operation is multiplication
/
The operation is division
%
The operation is modulus
$
Incorrect operator
102
Aryaf Aladwan
while Repetition Structure
• Repetition structure
– Action repeated while some condition remains true
– while loop repeated until condition becomes false
– Structure:
while (condition)
{ true
condition statements
statements;
counter ++; false
103
Aryaf Aladwan
#include <iostream.h> #include <iostream.h> #include <iostream.h>
void main() void main() void main()
{ { {
int counter=1; int counter=1; int counter=1;
while (counter<=10) while (counter<=10) while (counter++<=10)
{ { {
cout<<counter<<" "; counter++; cout<<counter<<" ";
counter++; cout<<counter<<" "; }
} } }
} }
1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11
104
Aryaf Aladwan
#include <iostream.h> #include <iostream.h> #include <iostream.h>
void main() void main() void main()
{ int counter=1; { int counter=10; {
while (counter<=10) while (counter>0) int counter=1;
{ { while (counter<=10)
cout<<counter<<" "; cout<<counter<<" "; { counter++;
counter++; counter--; cout<<counter<<" ";
} } counter++;
cout<<counter; } }
} }
1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 2 4 6 8 10
105
Aryaf Aladwan
#include <iostream.h>
void main() Write a program
{ to find the
int p=0,f=0; average of 10
int total; marks?
int gradeCounter;
int grade;
float average;
total = 0;
gradeCounter = 1; Enter grade: 77
while ( gradeCounter <= 10 ) Enter grade: 65
{ Enter grade: 41
cout << "Enter grade: "; Enter grade: 80
cin >> grade; Enter grade: 60
total = total + grade; Enter grade: 78
if(grade>=50) Enter grade: 79
Enter grade: 40
p++;
Enter grade: 50
else
Enter grade: 88
f++;
Class average is 65
gradeCounter = gradeCounter + 1;
number of failures: 2
} number of passers: 8
average = total / 10;
cout << "Class average is " << average << endl;
cout<<"number of failures: "<<f<<endl;
cout<<"number of passers: "<<p;
106
}
Write a program which calculate the sum of the following sequence
1 1 1 1
H =1 + + + + ....... +
2 3 4 n
#include <iostream.h>
void main()
OUTPUT
{
float H=0.0; 2.92897
107
Aryaf Aladwan
do/while Repetition Structure
true
condition
false
108
Aryaf Aladwan
#include <iostream.h> #include <iostream.h> #include <iostream.h>
void main() void main() void main()
{ { {
int counter=1; int counter=1; int counter=1;
do do do
{ { {
cout<<counter<<" "; counter++; cout<<counter<<" ";
counter++; cout<<counter<<" "; }while (counter++<=10);
}while (counter<=10); }while (counter!=7); }
} }
1 2 3 4 5 6 7 8 9 10 234567 1 2 3 4 5 6 7 8 9 10 11
109
Aryaf Aladwan
#include <iostream.h> #include <iostream.h>
void main() void main()
{ {
char counter=‘b'; int counter=8;
do do
{ {
cout<<counter<<" "; counter++;
counter--; cout<<counter<<" ";
} while (counter>'z'); counter++;
} } while (counter<=10);
}
OUTPUT OUTPUT
b 9 11
110
Aryaf Aladwan
#include<iostream.h>
void main()
{
int A =3;
char g ='f';
do{
A++;
if (g=='m')
cout<<g<<endl;
else
cout<<++A<<endl;
g ='m';
}while (A<8);
cout<<"thx";
OUTPUT 5
m
m
m
thx
111
112
Aryaf Aladwan
break and continue Statements
• break statement
– Immediate exit from while, for, do/while, switch
– Program continues with first statement after structure
• Common uses
– Escape early from a loop
– Skip the remainder of switch
113
Aryaf Aladwan
break statement
#include <iostream.h>
void main()
{
for ( int x = 1; x <= 10; x++ )
{
// if x is 5, continue with next iteration of loop
if ( x == 5 )
break; // skip remaining code in loop body
cout << x << " "; // display value of x
} // end for structure
cout<<" \n broke out of loop at x of "<<x;
}
OUTPUT
1 2 3 4
broke out of loop at x of 5
114
115
Aryaf Aladwan
116
Aryaf Aladwan
break and continue Statements
• continue statement
– Used in while, for, do/while
– Skips remainder of loop body
– Proceeds with next iteration of loop
• while and do/while structure
– Loop-continuation test evaluated immediately after the
continue statement
• for structure
– Increment expression executed
– Next, loop-continuation test evaluated
117
Aryaf Aladwan
continue statement
#include <iostream.h>
void main()
{
for ( int x = 1; x <= 10; x++ )
{
// if x is 5, continue with next iteration of loop
if ( x == 5 )
continue; // skip remaining code in loop body
cout << x << " "; // display value of x
} // end for structure
cout << " Used continue to skip printing the value 5 ";
}
OUTPUT
1 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5
118
119
Aryaf Aladwan
120
Aryaf Aladwan
121
Aryaf Aladwan
122
Aryaf Aladwan
Nested For
initialization F
T T
condition initialization condition statement increment
123
Aryaf Aladwan
124
Aryaf Aladwan
125
Aryaf Aladwan
126
Aryaf Aladwan
127
Aryaf Aladwan
128
Aryaf Aladwan
129
Aryaf Aladwan
Example 1
#include <iostream.h>
OUTPUT
void main()
{
i=1 j=1
int i, j; i=1 j=2
for (i=1;i<=4;i++) i=1 j=3
{ i=2 j=1
for (j=1;j<=3;j++) i=2 j=2
cout<<"i="<<i<<" j="<<j<<endl; i=2 j=3
} i=3 j=1
} i=3 j=2
i=3 j=3
i=4 j=1
i=4 j=2
i=4 j=3
130
Aryaf Aladwan
Example 2
#include <iostream.h>
void main()
{ OUTPUT
int i, j;
for (i=0;i<=4;i++)
*****
{
****
for (j=i;j<=4;j++)
***
cout<<"*";
**
cout<<endl;
*
}
}
131
Aryaf Aladwan
Example 3
#include <iostream.h>
void main()
{ OUTPUT
int i, j;
for (i=1;i<=5;i++)
*
{
**
for (j=1;j<=i; j++)
***
cout<<"*";
****
cout<<endl;
*****
}
}
132
Aryaf Aladwan
133
Aryaf Aladwan
End of ch2
134
Aryaf Aladwan