Top Banner
20061018 chap 5 Chapter 5 Repetition and Loop Statements
31

Chapter 5

Jan 15, 2016

Download

Documents

gavan

Chapter 5. Repetition and Loop Statements. Repetition in Programs. In most commercial software, you can repeat a process many times. When using an editor program, you can move the cursor to a program line and perform as many edit operations as you need to. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 5

20061018 chap5

Chapter 5

Repetition and Loop Statements

Page 2: Chapter 5

20061018 chap5 2

Repetition in Programs In most commercial software, you can

repeat a process many times. When using an editor program, you can

move the cursor to a program line and perform as many edit operations as you need to.

Loop is a control structure that repeats a group of steps in a program.

Three C loop control statements while for do-while

Page 3: Chapter 5

20061018 chap5 3

Flow Diagram of Loop Choice

Page 4: Chapter 5

20061018 chap5 4

Comparison of Loop Kinds

Page 5: Chapter 5

20061018 chap5 5

The while statement Counter-controlled loop (or counting

loop) A loop whose required number of iterations

can be determined before loop execution begins.

The syntaxwhile (loop repetition condition)

statement; Loop repetition condition: the condition

that controls loop repetition. Infinite loop: a loop that executes forever

Page 6: Chapter 5

20061018 chap5 6

Examplecount_emp = 0; /* no employees processed yet

*/while (count_emp < 7) { /* test value of

count_emp */ printf("Hours> "); scanf("%d", &hours); printf("Rate> "); scanf("%lf", &rate); pay = hours * rate; printf("Pay is $%6.2f\n", pay); count_emp = count_emp + 1; /* increment

count_emp */}printf("\nAll employees processed\n");

Page 7: Chapter 5

20061018 chap5 7

Flowchart for a while Loop

Page 8: Chapter 5

20061018 chap5 8

Computing a Sum or a Product in a Loop Loops often accumulate a sum

or a product by repeating an addition or multiplication operation.

Accumulator A variable used to store a value

being computed in increments during the execution of a loop.

Page 9: Chapter 5

20061018 chap5 9

Example

Page 10: Chapter 5

20061018 chap5 10

Compound Assignment Operators

• C provide special assignment operators variable op = expression;

Page 11: Chapter 5

20061018 chap5 11

The for Statement Three loop control components with

the loop body. Initialization of the loop control variable, Test of the loop repetition condition,

and Change (update) of the loop control

variable. The for statement in C supplies a

designed place for each of these three components .

Page 12: Chapter 5

20061018 chap5 12

Counting Loop by for Statement

Page 13: Chapter 5

20061018 chap5 13

for Statement

Page 14: Chapter 5

20061018 chap5 14

Increment and Decrement Operators

The increment (i.e., ++) or decrement (i.e., --) operators are the frequently used operators which take only one operand.

for(int i=0; i<100; i++) {…}

for(int i=100; i>0; i--) {…}

Side effect: the value of its operand is incremented/decremented by one.

Page 15: Chapter 5

20061018 chap5 15

Prefix and Postfix Increments

The value of the expression in which the ++/-- operator is used depends on the position of the operator.

Page 16: Chapter 5

20061018 chap5 16

Example: Factorial Function

Page 17: Chapter 5

20061018 chap5 17

Inc. and Dec. Other Than 1

Page 18: Chapter 5

20061018 chap5 18

Conditional Loops In many programming situations,

you will not be able to determine the exact number of loop repetitions before loop execution begins.

Example: Monitor Gasoline Storage Tank

(Fig. 5.9)

Page 19: Chapter 5

20061018 chap5 19

Page 20: Chapter 5

20061018 chap5 20

Page 21: Chapter 5

20061018 chap5 21

Loop Design Problem-Solving Questions for Loop Design

What are the inputs? What are the outputs? Is there any repetition? Do I know in advance how many time steps will

be repeated? How do I know how long to keep repeating the

steps? Sentinel-Controlled Loops Endfile-Controlled Loops Infinite Loops on Faulty Data

Page 22: Chapter 5

20061018 chap5 22

Sentinel-Controlled Loops

One way to do this is to instruct the user to enter a unique data value, called a sentinel value, after the last data item.(Fig. 5.10)

Page 23: Chapter 5

20061018 chap5 23

Endfile-Controlled Loops

A data file is always terminated by an endfile character that can be detected by the scanf and fscanf functions.

EOF stands for the endfile character.

Page 24: Chapter 5

20061018 chap5 24

Example of Endfile-Controlled Loops

Page 25: Chapter 5

20061018 chap5 25

Infinite Loops on Faulty Data 70 7o

Detect faulty data

Page 26: Chapter 5

20061018 chap5 26

Nested Loops Consist of an outer loop with one

or more inner loops. (Fig. 5.13)

Page 27: Chapter 5

20061018 chap5 27

The do-while Statement When we know that a loop

must execute at least one time.

Page 28: Chapter 5

20061018 chap5 28

Flag-Controlled Loops A flag is a variable used to represent

whether or not a certain event has occurred.(Fig. 5.14)

Page 29: Chapter 5

20061018 chap5 29

Page 30: Chapter 5

20061018 chap5 30

Homework #5 Due: 2006/10/25 利用迴圈和 '*' 可以畫出許多圖形

1. 定義一個 CENTER 常數 (#define CENTER 20) 表示所有圖形的中心位置

2. 實作出一個 function prototype 如下 : void triangle(int n); 其作用是繪出 n 排星號的等腰三角形 若 n 為正時,則畫出三角形是正立的,如 a. (n=4) 若 n 為負時,則畫出三角形是倒立的,如 b. (n=-4)

3. 主程式將輸入多個整數 ( 最後以 0 結尾 ) ,印出其對應的圖案 並判斷每個整數若不在 -9 與 9 的範圍內則不予理會之

a. b. * ******* *** ***** ***** *** ******* *

Page 31: Chapter 5

20061018 chap5 31

Summary Repetition and Loop Statements

Counter-Controller Loop Sentinel-Controller Loop Endfile-Controller Loop Input Validation Loop General Conditional Loop

while, for, and do-while Compound Assignment Operators