Chapter 6 Control Structures
Chapter 6 Control Structures
Chapter 6 Control Structures
AIM
To acknowledge student with the concept of
control structures in programming: selection
control structure and repetition control
structure
OBJECTIVES
At the end of the chapter, student will know:
To explain control structures concept
To write a C++ program using control structures
CONTROL
STRUCTURE
Sequence
Control
Structure
Repetition
Control
Structure
Selection
Control
Structure
Selection Control
Structure
Used in programming to:
Selection Control
Structure
Best example/case:
Selection Control
Structure
Selection Control
Structure
How decision is made when user
make their selection from the
ATM menu?
The system will check the input and
compares
the
selection
with
the
conditions set in the system. If the input
matches the setting conditions, the
corresponding menu will be displayed.
6
Selection Control
Structure
Choose only one instruction to be executed.
Selection will be done if only the conditional
statement is TRUE.
Types of selection control structure:
Single
selection
(if)
Double
selection
(if-else)
Multi level
selection
(if-else-if)
7
i) Single Selection
cont
Convert to flowchart:
grade ==A
Yes
Print Excellent
No
Reserved
word
if (conditional expression)
C++ statement;
Consist of:
Output statement/
Input statement/
expression
if ( grade = = A)
cout << Excellent ;
Conditional
expression
Yes
grade = A
Print Passed
No
Print Failed
if (conditional statement)
C++ statement;
else
C++ statement;
if ( grade ==A)
cout << Passed;
else
cout << Failed;
15
One
Two
Three
Four
Five
cont
40-59
0-39
cont
cont
Example:
void main()
{
int number;
cout<<Please insert a number = \n;
cin>>number;
variable
switch(number)
{
case 1 : cout << One\n;
break;
The value
case 2 : cout << Two\n;
of
break;
variablecase 3 : cout << Three\n;
number
break;
default: cout << Others;
} }
UNIVERSITI TUN HUSSEIN ONN MALAYSIA
Action
void main()
{
int number;
cin>>number;
cont
void main()
{
int number;
cin>>number;
if (number == 1)
switch(number)
cout<< One\n;
{
case 1 : cout<<One\n;else if (number == 2)
cout<< Two\n;
break;
else if (number == 3)
case 2 : cout<< Two\n;
cout<< Three\n;
break;
else
case 3 : cout<< Three\n;
cout<< Others;
break;
}
default: cout<< Others;
}
}
Exercise
Convert the following if..else statement
to switch..case statement
if (colour == r)
cout<<endl<<red symbolise bravery;
else if(colour == b)
cout<< endl<< blue symbolises unity;
else if(colour == k)
cout<< endl<< yellow is a royal colour;
else
cout<< endl<< Error;
{
int r, b, k;
char color;
cout<<"Enter one color = ";
cin>>color;
switch(color)
{
case 'r' :
bravery\n";
case 'b' :
case 'k' :
break;
cout << "blue symbolise unity\n";
break;
cout << "yellow is a royal
colour\n";
break;
default: cout << "Others";
Exercise
cont
cont
{
int nom1, nom2, nom3, nom4, nom5;
cout<<"Enter one number = ";
cin>>nom1;
cout<<"Enter one number = ";
cin>>nom2;
cout<<"Enter one number = ";
cin>>nom3;
cout<<"Enter one number = ";
cin>>nom4;
cout<<"Enter one number = ";
cin>>nom5;
cout << endl << endl;
system("pause");
return 0;
} UNIVERSITI TUN HUSSEIN ONN MALAYSIA
Repetition Control
Structure
i.While Loop
ii.Do While Loop
iii.For Loop
Repetition Control
Structure
Enable statements to be
repeated.
While the value of conditional
expression is TRUE,
statements will be repeated.
Repetition will be terminated if
the conditional expression is
(FALSE).
Type of repetition/loop control
While loop
Use while statement for pre
test loop. When the condition
becomes false, the while
loop is exited.
Entry controlled loop
because
Repeated
statements
counter = 0;
condition is
checked
before
while
(conditional expression)
{
the statements
in the loop
C++ statement;
counter++;
are executed.
}
IMPORTANT!
1. Counter
2. Counter initialization
3. Conditional expression testing
4. Increment/decrement of counter
While loop
Example:
While loop
Example:
nom = 10;
While i < 20
{ cout << \t<<bil;
nom++;
}
10
11
12
13
14
15
16
17
18
19
While loop
Example:
Dowhile loop
Use do..while statement for post
test loop. The condition checked at
the end of loop.
Repetition will occurred if condition
is TRUE otherwise the loop is exited
if the condition
FALSE.
counter is
= 0;
Similar todowhile, must include 3
{ C++ statement;
important things
counter++;
Repeated statements
} while (x < 5);
Format :
Dowhile loop
Example:
Dowhile loop
Example:
nom = 10;
do
{ cout << \t<<bil;
nom--;
}
while (bil > 5);
10 9 8 7 6
Example
:
while
do..while
FOR LOOP
For Loop
The statements in the for loop repeat continuously for
aspecific number of times. The while and do-while loops
repeat until a certain condition is met.
The for loop repeats until a specific count is met.
Use a for loop when the number of repetition is know, or
can be supplied by the user.
The coding format is: for(startExpression;
testExpression; countExpression)
{
block of code;
}
For Loop
The startExpression is evaluated before the loop begins.
It is acceptable to declare and assign in the
startExpression (such
as int
x
=
1;).
This
startExpression is evaluated only once at the beginning of
the loop.
The testExpression will evaluate to TRUE (nonzero) or
FALSE (zero). While TRUE, the body of the loop
repeats. When the testExpression becomes FALSE, the
looping stops and the program continues with the
statement immediately following the for loop body in the
program code.
CONCLUSION
Selection structure
i. Single(if)
ii. Double(else)
iii. Multiple(else if)
. Repeatation (loop)
i. While
ii. While..doo
iii. For..loop