You are on page 1of 45

Chapter 4 Control Structure: Loop

Prof. Kuanquan Wang


The School of Computer Science and Technology
Harbin Institute of technology

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

Outline
4.1 Review and introduction to loop flow control structure 4.2 Loop control with counter 4.3 Loop control with condition test 4.4 Loop control with sentinel

4.1 Review and introduction to loop flow control structure

How many basic flow control structure?

There are 3 kinds of basic flow control structure.

Sequence

Selection

Repetition or loop

How Loops are Controlled?

Counter Controlled 1, 2, 3, 4, , 4, 3, 2, 1
C Programming Language

Sentinel Controlled Condition Controlled


4

4.2 Counter Controlled Loop


counter initial Value
false test counter value true

Step x
Update counter Step n

C Programming Language

Counter Controlled Loop


counter counter = initialValue initial Value
false test counter value true

Step x
Update counter Step n

C Programming Language

Example:
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

Can you identify the input and output???

Input example: 2 3 4 5 6
Output example: 20
C Programming Language 7

Assume input example: 2 3 4 5 6

start
Input n1 n1 n2 n3 n4 n5 sum 2 3 4 5 6 20

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

Input n2 Input n3 input n4 input n5 sum n1+n2+n3+n4+n5 output sum

C Programming Language

end

CounterAssume input Controlled example: 2 3 4 5 6 Loop

counter 1, sum 0
false

counter sum

1 2 3 4 5 6 14 20 0 2 5 9
14 0+ 2 5 9 +5 2 3 4 6

counter < 6 true input n sum sum + n

6 1 2 3 5 4 < < < 6 6 6

false true true

2 3 4 5 6

counter++ output sum


C Programming Language

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

Decreasing Counter-Controlled Loop counter 5, sum 0


false

counter > 0
true

input x sumsum+ x counter-output sum


C Programming Language 10

Loop : for

Condition is tested first Loop is controlled by a counter Syntaxes for (initial value ; condition; update counter) statement;
Or

for (initial value ; condition; update counter) { statement; statement; }


C Programming Language 11

i 0, sum 0
false

i< 5
true

input x sumsum+ x

i++
output sum
C Programming Language

int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(%d,&x); sum = sum + x; } printf(%d,sum);
12

for statement
???

Example: for ( num = 1; num <= 3; num++ ) printf(%d\t, num);


1 _ come to exit\n); printf(have

num

C Programming Language

13

for statement
1

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


_

C Programming Language

14

for statement
1

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


_

C Programming Language

15

for statement
1

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 _

C Programming Language

16

for statement
2

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 _

C Programming Language

17

for statement
2

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 _

C Programming Language

18

for statement
2

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 2 _

C Programming Language

19

for statement
3

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 2 _

C Programming Language

20

for statement
3

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 2 _

C Programming Language

21

for statement
3

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 2 3 _

C Programming Language

22

for statement
4

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num);

num

printf(have come to exit\n);


1 2 3 _

C Programming Language

23

for statement
4

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num); printf(have come to exit\n);

num

C Programming Language

24

for statement
4

Example: for (num = 1; num <= 3; num++ ) printf(%d\t, num); printf(have come to exit\n);

num

have come to exit_

C Programming Language

25

4.3 Loop control with condition test

Loop Structure

Condition is tested first

Condition is tested later

C Programming Language

26

Testing Condition First


Step a
false

condition
true

Step x Step y Step n


C Programming Language 27

Testing Condition First


Step a

condition

Step x Step y Step n


C Programming Language 28

Loop: while
Condition is tested first Loop is controlled by condition or a counter Syntax while (condition) statement; Or while (condition) { statement; statement; }

C Programming Language

29

Testing condition later


Step a Step x Step y
true

condition
false

Step n
C Programming Language 30

Testing condition later


Step a Step x Step y
true

condition
false

Step n
C Programming Language 31

Do-while Loop

Statements in the loop are executed first (at least once), and condition is tested last Loop is controlled by a condition or counter Syntax do { statement; statement; } while (condition); statement;
C Programming Language 32

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

C Programming Language

33

Condition-Controlled Loop
m 110 57 5

Assume m=110 m=57 m=5 input m

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

m<0 || m>100
false

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

true

m>49
false

PASS

FAIL

WRONG INPUT FAIL PASS

C Programming Language

34

input m
false

m<0 || m>100
true

WRONG INPUT

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

true

m>49
false

PASS

FAIL
35

int marks; scanf(%d,&marks); while (marks<0) | | (marks>100) { printf(WRONG INPUT); scanf(%d,&marks); } if (marks>49) { printf(PASS); else printf(FAIL); }
C Programming Language

Double Selection

36

do-while statement

??? 65 66 67 68

??? 67

start

end

Example : printf(Input start and end value : ); scanf(%d %d, &start, &end); do { printf(%c (%d)\n, start, start); start++; 66 <= 67 67 } while (start <= end) ; 68

_ Input start and end value : 65 _ 67 67_ A (65) _ (66) B _ (67) C Programming Language C
_

37

4.4 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
C Programming Language

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

38

Input Example: 30 16 42 -9 Output Example: Sum = 88 Average = 29.33

Sentinel Value

C Programming Language

39

Now What have you understand?

C Programming Language

40

Try to understand

sum0

What will happen if this statement is deleted???

input x
false

x>0
true

input x sumsum+x

What happened if these 2 statements exchange places

input x sum sum+x


display sum
C Programming Language

?
41

Exercise

Given a set of integers with the last one being 999 Display the summation of all the integers.

Draw the flowchart for this problem

Input example: 1 3 23 999 Output example: Sum = 27


C Programming Language 42

Sentinel-controlled loop
sum=0 input x
false #include <stdio.h> void main() { int sum, x; sum = 0; scanf(%d, &x); while (x != 999) {

x!=999
true

sum = sum + x;
scanf(%d, &x); }

sumsum+x input x output sum


C Programming Language

printf(The sum : %d\n, sum);


}
43

int sum, x; sum = 0; scanf(%d, &x); 3!= 999 != != 999 999 999 != 999 while (x != 999) { 123 sum = sum + x; scanf(%d, &x); } printf(\nThe sum : %d\n, sum);

? 999 23 1 3

sum

? 4+23 0+1 1+3 27 0 1 4

_ 1

23

999
44

The sum : 27
C Programming Language

Now Let us have a summary! The concept of Loop Structure


3 methods to control loop structure
Counter Controlled, Condition Controlled, Sentinel Controlled

3 statements in C language
for, while, do while

Thank you for your attention!


Now let us have a break!
C Programming Language 45

You might also like