Slide 08a - Control Structure_Loop

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 1

description

Slide 08a - Control Structure_Loop

Transcript of Slide 08a - Control Structure_Loop

Page 1: Slide 08a - Control Structure_Loop

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

1

Page 2: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 22

Loop StructureLoop Structure

Condition is tested first Condition is tested later

Page 3: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 33

false

true

Testing Condition First

condition

Step x

Step y

Step a

Step n

Page 4: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 44

Testing Condition First

condition

Step x

Step y

Step a

Step n

Page 5: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 55

Step a

condition

Step n

Step x

false

true

Step y

Testing condition later

Page 6: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 66

Step a

condition

Step n

Step x

false

true

Step y

Testing condition later

Page 7: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 77

How Loops are ControlledHow Loops are Controlled

Sentinel Controlled

Counter Controlled•1, 2, 3, 4, …•…, 4, 3, 2, 1

Condition Controlled

Page 8: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 88

Counter Controlled LoopCounter Controlled Loop

counter ← initialValue

test counter value

Step n

Step x

false

true

Update counter

Page 9: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 99

Counter Controlled LoopCounter Controlled Loop

counter = initialValue

test counter value

Step n

Step x

false

true

Update counter

counter ← initialValue

Page 10: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1010

Example:Example:Can you

identify the input and output???

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

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

Page 11: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1111

Input n1

Input n2

Input n3

input n4

input n5

output sum

sum ← n1+n2+n3+n4+n5

start

2n1

Assume input example: 2 3 4 5 6

3n2

4n3

5n4

6n5

20sum

end

This flowchart does not use loop, hence

we need to use 6 different variables

Page 12: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1212

Counter-Counter-Controlled Controlled LoopLoop

counter ← 1, sum ← 0

counter < 6

sum ← sum + n

false

true

counter++

output sum

input n

1counter

sum 0

1 < 6 true

2n

0 + 22

2

2 < 6 true

3

2 + 35

3

3 < 6 true

4

5 + 49

4

4 < 6 true

5

9 + 514

5

5 < 6 true

6

14 + 620

6

6 < 6 false

Assume input example:

2 3 4 5 6

This loop iscounter-controlled

The counter Increases by 1

Uses only 3 variables

Page 13: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1313

Decreasing Counter-Controlled LoopDecreasing Counter-Controlled Loop

counter ← 5, sum ← 0

counter > 0

sum←sum+ x

false

true

counter--

output sum

input x

Page 14: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1414

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

Page 15: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1515

Condition-ControlledCondition-Controlled LoopLoop

false

true

input m

m<0 || m>100

m>49 “PASS”

“FAIL”

true

false

“WRONG INPUT”

Assume m=110

m 110

110 < 0 || 110 >100

WRONG INPUT

Assume m=5

5

5 < 0 || 5 >100

5 > 49

FAIL

Assume m=57

57

57 < 0 || 57 >100

57 > 49

PASS

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

Page 16: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1616

false

true

input m

m<0 || m>100

m>49 “PASS”

“FAIL”

true

false

“WRONG INPUT”

input m

Condition-controlled loop with its condition being tested first

Page 17: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1717

Sentinel-Controlled LoopSentinel-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

Can you identify the input and output???

Input: A set of integers ending with a

negative integer or a zero

Output: Summation and Average of these integers

Page 18: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1818

Input Example:30 16 42 -9

Output Example:Sum = 88Average = 29.33

Sentinel Value

Page 19: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 1919

Now…What have you

understand?

Page 20: Slide 08a - Control Structure_Loop

TK1913-C ProgrammingTK1913-C Programming 2020

x>0

sum←sum+x

false

true

input x

sum←0

input x

display sum

Try to understand What will happen if this statement is deleted???

?

What happened if these 2

statements exchange

places

sum←sum+x

input x

?