Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
76 views20 pages

Slide 08a - Control Structure - Loop

The document discusses different types of loop control structures in C programming including counter-controlled loops, condition-controlled loops, and sentinel-controlled loops. It provides examples of flowcharts to illustrate how to implement programs using these different loop structures, including examples that read multiple integer inputs, calculate a summation and average, and display output based on a condition.

Uploaded by

EffendyFooad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views20 pages

Slide 08a - Control Structure - Loop

The document discusses different types of loop control structures in C programming including counter-controlled loops, condition-controlled loops, and sentinel-controlled loops. It provides examples of flowcharts to illustrate how to implement programs using these different loop structures, including examples that read multiple integer inputs, calculate a summation and average, and display output based on a condition.

Uploaded by

EffendyFooad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Control Structure:

Loop
(Part 1)
Knowledge:
Understand the various concepts of loop control structure
Skill:
Be able to develop a program involving loop control structure

Loop Structure

Condition is tested first

TK1913-C Programming

Condition is tested later

Testing
Condition First
Step a
false

condition
true

Step x
Step y
Step n
TK1913-C Programming

Testing
Condition First
Step a

condition

Step x
Step y
Step n
TK1913-C Programming

Testing condition
later
Step a
Step x
Step y
true

condition
false

Step n
TK1913-C Programming

Testing condition
later
Step a
Step x
Step y
true

condition
false

Step n
TK1913-C Programming

How Loops are Controlled

Condition
Controlled

TK1913-C Programming

Sentinel
Controlled
Counter Controlled
1, 2, 3, 4,
, 4, 3, 2, 1
7

Counter Controlled Loop


counter initialValue
false
test counter
value
true

Step x
Update counter
Step n

TK1913-C Programming

Counter Controlled Loop


counter
counter
= initialValue
initialValue
false
test counter
value
true

Step x
Update counter
Step n

TK1913-C Programming

Example:
Draw a flowchart for the
following problem:
Read 5 integer and display the
value of their summation.

Can you
identify the
input and
output???

Input :

5 integer
n1, n2, n3, n4, n5
Output: The summation of
n1, n2, .., n5

Input example: 2 3 4 5 6
Output example: 20
TK1913-C Programming

10

start

Assume input example:


2 3 4 5 6

n1

Input n2

n2

Input n3

n3

input n4

n4

n5

sum

20

Input n1
This flowchart does
not use loop, hence
we need to use 6
different variables

input n5
sum n1+n2+n3+n4+n5
output sum
TK1913-C Programming

end

11

CounterAssume input
Controlled
example:
2 3 4 5 6
Loop

counter 1, sum 0

1
2
3
4
5
6

counter

14
20
0
2
5
9

sum
false

counter < 6

65
14<<<666
2
3

14
0 ++ 5
2
5
9
26
3
4

false
true
true

true
input n

2
3
4
5
6

sum sum + n

counter++

This
loop
Uses
only is
The
counter
counter-controlled
3 variables
Increases
by 1

output sum
TK1913-C Programming

12

Decreasing Counter-Controlled Loop


counter 5, sum 0
false

counter > 0
true

input x
sumsum+ x
counter-output sum
TK1913-C Programming

13

Example: Draw a flowchart for this problem;


Given an exam marks as input, display the
appropriate message based on the rules below:

If marks is greater than 49, display PASS,


otherwise display FAIL
However, for input outside the 0-100 range,
display WRONG INPUT and prompt the
user to input again until a valid input is
entered

TK1913-C Programming

14

Assume
m=110
m=57
m=5

Condition-Controlled Loop
m

110
57
5

input m

WRONG INPUT
true
110
5
57
<<0<0||0||5||57
>100
110
>100
>100

m<0 || m>100
false

557
>Condition-controlled
49
> 49
loop with its condition
being tested at the end

true

m>49

PASS

false

FAIL

WRONG INPUT
FAIL
PASS

TK1913-C Programming

15

input m
false

m<0 || m>100
true

WRONG INPUT

input m
Condition-controlled
loop with its
condition being
tested first
TK1913-C Programming

true

m>49

PASS

false

FAIL
16

Sentinel-Controlled Loop
Draw a flowchart for a
problem which:
Receive a number of
positive integers and
display the summation
and average of these
integers.
A negative or zero input
indicate the end of input
process
TK1913-C Programming

Can you
Input: A set of identify
integers the
ending with a
input and
negative integer or a zero
output???

Output: Summation and


Average of these integers

17

Input Example:
30 16 42 -9

Sentinel
Value

Output Example:
Sum = 88
Average = 29.33

TK1913-C Programming

18

Now
What have you
understand?

TK1913-C Programming

19

Try to understand

What will
happen if this
statement is
deleted???

sum0

input x
false

x>0
true

input x
sumsum+x

input x
sumsum+x

What happened
if these 2
statements
exchange
places

display sum
TK1913-C Programming

20

You might also like