45
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Prof. Kuanquan Wang The School of Computer Science and Technology Harbin Institute of technology

Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

Embed Size (px)

Citation preview

Page 1: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

Chapter 4 Control Structure: Loop

Knowledge:Understand the various concepts of loop control structure

Skill:Be able to develop a program involving loop control structure

1

Prof. Kuanquan Wang

The School of Computer Science and Technology

Harbin Institute of technology

Page 2: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

Outline

4.1 Review and introduction to loop flow control structure

4.2 Loop control with counter4.3 Loop control with condition test4.4 Loop control with sentinel

Page 3: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

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

Page 4: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 44

How Loops are Controlled?How Loops are Controlled?

Sentinel ControlledCounter Controlled

•1, 2, 3, 4, …•…, 4, 3, 2, 1

Condition Controlled

Page 5: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 55

4.2 Counter Controlled Loop4.2 Counter Controlled Loop

counter ← initial Value

test counter value

Step n

Step x

false

true

Update counter

Page 6: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 66

Counter Controlled LoopCounter Controlled Loop

counter = initialValue

test counter value

Step n

Step x

false

true

Update counter

counter ← initial Value

Page 7: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 77

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 8: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 88

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 9: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 99

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 10: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1010

Decreasing Counter-Controlled LoopDecreasing Counter-Controlled Loop

counter ← 5, sum ← 0

counter > 0

sum←sum+ x

false

true

counter--

output sum

input x

Page 11: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1111

Loop : Loop : forfor

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;

}

Page 12: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1212

i ← 0, sum ← 0

i < 5

sum←sum+ x

false

true

i++

output sum

input x

int x, sum, i;sum = 0;for (i = 0; i < 5; i++) {

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

}printf(“%d”,sum);

Page 13: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1313

forfor statement statement

Example:

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

num

???

_

1 _printf(“have come to exit\n”);

Page 14: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1414

Example:

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

num

1

_

forfor statement statement

printf(“have come to exit\n”);

Page 15: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1515

Example:

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

num

1

_

forfor statement statement

printf(“have come to exit\n”);

Page 16: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1616

Example:

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

num

1

1 _

forfor statement statement

printf(“have come to exit\n”);

Page 17: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1717

Example:

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

num

2

1 _

forfor statement statement

printf(“have come to exit\n”);

Page 18: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1818

Example:

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

num

2

1 _

forfor statement statement

printf(“have come to exit\n”);

Page 19: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 1919

forfor statement statement

Example:

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

num

2

1 2 _

printf(“have come to exit\n”);

Page 20: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2020

Example:

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

num

3

1 2 _

forfor statement statement

printf(“have come to exit\n”);

Page 21: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2121

Example:

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

num

3

1 2 _

forfor statement statement

printf(“have come to exit\n”);

Page 22: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2222

Example:

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

num

3

1 2 3 _

forfor statement statement

printf(“have come to exit\n”);

Page 23: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2323

Example:

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

num

4

1 2 3 _

forfor statement statement

printf(“have come to exit\n”);

Page 24: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2424

Example:

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

printf(“have come to exit\n”);

num

4

1 2 3 _

forfor statement statement

Page 25: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2525

Example:

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

printf(“have come to exit\n”);

num

4

1 2 3 have come to exit_

forfor statement statement

Page 26: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2626

Loop StructureLoop Structure

Condition is tested first Condition is tested later

4.3 Loop control with condition test

Page 27: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2727

false

true

Testing Condition First

condition

Step x

Step y

Step a

Step n

Page 28: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2828

Testing Condition First

condition

Step x

Step y

Step a

Step n

Page 29: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 2929

Loop: whileLoop: while

Condition is tested first Loop is controlled by condition or a counter Syntax

while (condition) statement;

Orwhile (condition) {

statement;statement;

}

Page 30: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3030

Step a

condition

Step n

Step x

false

true

Step y

Testing condition later

Page 31: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3131

Step a

condition

Step n

Step x

false

true

Step y

Testing condition later

Page 32: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3232

Do-while LoopDo-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;

Page 33: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3333

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 34: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3434

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 35: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3535

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 36: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3636

int marks;scanf(“%d”,&marks);while (marks<0) | | (marks>100) {

printf(“WRONG INPUT”);scanf(“%d”,&marks);

}if (marks>49) {

printf(“PASS”);else

printf(“FAIL”);}

DoubleSelection

Page 37: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3737

do-while statementdo-while statement

Example :printf(“Input start and end value : “);scanf(“%d %d”, &start, &end);do {

printf(“%c (%d)\n“, start, start);start++;

} while (start <= end) ;

_

???

start

???

end

Input start and end value : _Input start and end value : 65 67_

65 67

Input start and end value : 65 67A (65)_

66

66 <= 67

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

67 <= 67

67

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

68

68 <= 67

Page 38: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3838

4.4 Sentinel-Controlled Loop4.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

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 39: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 3939

Input Example:

30 16 42 -9

Output Example:

Sum = 88

Average = 29.33

Sentinel Value

Page 40: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 4040

Now…What have you

understand?

Page 41: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 4141

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

?

Page 42: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 4242

ExerciseExercise Given a set of integers

with the last one being 999 Display the summation of

all the integers.

Input example: 1 3 23 999

Output example: Sum = 27

Draw the flowchart for this problem

Page 43: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 4343

Sentinel-controlled loop

true

x!=999

sum←sum+x

false

input x

sum=0

input x

output sum

#include <stdio.h>

void main() {

int sum, x;

sum = 0;

scanf(“%d”, &x);

while (x != 999) {

sum = sum + x;

scanf(“%d”, &x);

}

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

}

Page 44: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 4444

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

?

?x

sum 0

1

_1

1 != 999

0+11

1 3

33 != 999

1+34

1 3 23

2323 != 999

4+2327

1 3 23 999

999999 != 999

The sum : 27

Page 45: Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop

C Programming LanguageC Programming Language 4545

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!