Sie sind auf Seite 1von 45

CONTROL STRUCTURE

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

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

CONTROL
STRUCTURE
Sequence
Control
Structure

Repetition
Control
Structure
Selection
Control
Structure

Selection Control
Structure
Used in programming to:

offer choice of interests to user of a system.


restrict/limit to only one operation/process
in a system to be executed at a time
allow user to choose only one selection of
process/operation at a time from a system.
execute a process/operation based on
selection.
3

Selection Control
Structure
Best example/case:

In general, the ATM processes are controlled


by some restrictions or rules which is written
using selection control structure (using
particular programming language).
The concept is "one selection, one
operation".

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

Single selection (if)


Use if statement.
Used in a situation which
if conditional statement is TRUE,
statement will be selected.

i) Single Selection
cont
Convert to flowchart:

grade ==A

Yes

Print Excellent

No

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

Single selection (if)


FORMAT

Reserved
word

if (conditional expression)
C++ statement;

Consist of:

Output statement/
Input statement/
expression

Single selection (if)


EXAMPL
E
If grade is A, print a message Excellent
Convert to valid selection statement:

if ( grade = = A)
cout << Excellent ;

Conditional
expression

Double selection (if-else)


Use if- else statement.
Use in situation which provide 2
condition, but only 1 selection can be
made.
If the condition is TRUE, then the first
statement will be executed.
If not, the next selection will be
executed.
12

ii) Double Selection


cont
Convert to flow chart:

Yes
grade = A

Print Passed
No

Print Failed

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

Double selection (if-else)


FORMAT

if (conditional statement)
C++ statement;
else
C++ statement;

Double selection (if-else)


EXAMPL
E
If grade == A, print a message You passed
Otherwise, print a message You failed
Convert to a valid C++ selection statement:

if ( grade ==A)
cout << Passed;
else
cout << Failed;
15

iii) Multi Level Selection


Use if..else if statement
Use in a situation which requires/provides
two choices.
If the conditional is TRUE (1), then selection
will be made.
Format: if (conditional expression is TRUE)
C++ statement;
else if (conditional expression is TRUE)
C++ statement;
else if (conditional expression is TRUE)
C++ statement;
else
C++ statement;
UNIVERSITI TUN HUSSEIN ONN MALAYSIA

iii) Multi Level Selection


cont
Example 1:
Write the selection control structure
statements in C++ to produce the
following output based on the input:
Input
Output
1

One

Two

Three

Four

Five

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

iii) Multi Level Selection


Answer:
if (input == 1)
cout << "=One\n";
else if (input == 2)
cout << "\n=Two\n";
else if (input == 3)
cout << "\n=Three\n";
else if (input == 4)
cout << "\n=Four\n";
else if (input == 5)
cout << "\n=Five\n";
else
cout << "\nInvalid\n";
cout << endl << endl;
system("pause");
return
0; TUN HUSSEIN ONN MALAYSIA
UNIVERSITI

cont

iii) Multi Level Selection


cont
Example 2:
Write the C++ statements to
determine students grade based on
their marks as shown in the following
table:
Mark
Grade
80-100
A
60-79

40-59

0-39

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

iii) Multi Level Selection


cont
Answer:
if (mark >=80 && mark <=100)
grade = A;
else if (mark >=60 && mark <=79)
grade = B;
else if (mark >= 40 && mark <= 59)
grade = C;
else if (mark >= 0 && mark <= 39)
grade = D;
else

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

iii) Multi Level Selection


switch..case statement

cont

Use for multi level selection


Similar to if..else..if but use different
format
Only Integer/
Format:
character
value

switch( conditional expression/variable)


{
case label1: C++ statement;
Exit from
break;
option
case label2: C++ statement;
break;

default: C++ statement;


}
Functioning like
else

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

iii) Multi Level Selection


switch..case statement

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

iii) Multi Level Selection


switch..case statement

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;
}
}

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

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;

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

{
int r, b, k;
char color;
cout<<"Enter one color = ";
cin>>color;
switch(color)
{
case 'r' :

cout << "red symbolise

bravery\n";
case 'b' :
case 'k' :

break;
cout << "blue symbolise unity\n";
break;
cout << "yellow is a royal

colour\n";
break;
default: cout << "Others";

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

Exercise
cont

How will you solve


this?
Write a C++ program which
will accept five integer
numbers

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

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:

Infinite 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

Infinite Dowhile loop


Example:

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.

The countExpression executes after each trip through the


loop. The count may increase/decrease by an increment of
1 or of some other value.
Braces are not required if the body of the for loop consists
of only ONE statement. Please indent the body of the loop
for readability
CAREFUL: When a for loop terminates, the value stored
in the computer's memory under the looping variable will
be "beyond" the testExpression in the loop. It must be
sufficiently large (or small) to cause a false condition.
Consider:
for (x = 0; x <= 13; x++)
cout<<"Melody";

When this loop is


finished, the value 14 is
stored in x.

CONCLUSION
Selection structure
i. Single(if)
ii. Double(else)
iii. Multiple(else if)
. Repeatation (loop)
i. While
ii. While..doo
iii. For..loop

Kari udang kari ketam


Enak dimakan di tepi pantai
Kalau korang masih tak faham
Harap kawan sebelah jangan dibantai..
Sekian..

Das könnte Ihnen auch gefallen