Top Banner
Loops or Iteration
72
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: LOOPING

Loops or Iteration

Page 2: LOOPING

8/10/2015 2

Loops or Iteration

A loop is a structure

that will execute a

group of lines of code

a number (i.e., zero to

many) times based on

predetermined criteria

Page 3: LOOPING

8/10/2015 3

Loops All languages need an iteration

statement

Recall that a Structured program

is made up of only the three

constructs:

Sequence

Selection

Iteration

Page 4: LOOPING

8/10/2015 4

Loops in C The purpose of a loop is to execute a

group of lines a number of times

The block is the unit that gets repeated.

Recall that a block is any number of

simple c statements surrounded by curly

braces.

a block can contain any number or type of

statements

a block can contain conditional logic, or

other loops

Page 5: LOOPING

8/10/2015 5

Loops Have:

Initialization

Test to exit the loop

Statements in the body of the loop

Increment or other statement that

will change the value that

determines whether you will stay in

the loop

some loops omit some of these

the order of these may vary

Page 6: LOOPING

8/10/2015 6

Statements that Build Loops in C

Three statement types

for

while

do while

Page 7: LOOPING

8/10/2015 7

Under What Conditions Do You Use Each

for - when you want to initialize a

variable at the beginning of the loop

and increment it after each iteration

while -when initialization and/or

increments are not done by the loop.

This is the "test before" loop

do / while -same as while but used

when you want to execute the body of

the loop at least once. This is the

"test after" loop

Page 8: LOOPING

8/10/2015 8

The for loop

A specially designed count-controlled

loop

for (initialization; test; updatecounter)

statement;

for (initialization; test; updatecounter)

{

statement1;

statement2;

. . .

}

Page 9: LOOPING

8/10/2015 9

The for loop

Actions of the for spread out

around the loop

initialization occurs only ONCE

at the start

testing is the first repeated

action of the loop (it gets done

before the body each iteration)

updatecounter occurs at the end

of the loop body

Page 10: LOOPING

8/10/2015 10

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“%d\n”,count);

Example

Initialization Test Updatecounter

End

Body

Page 11: LOOPING

11

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

Page 12: LOOPING

8/10/2015 12

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

1

Page 13: LOOPING

8/10/2015 13

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

1

True

Page 14: LOOPING

8/10/2015 14

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

1

1

Page 15: LOOPING

8/10/2015 15

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

2

1

Page 16: LOOPING

8/10/2015 16

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

2

True

1

Page 17: LOOPING

8/10/2015 17

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

2

12

Page 18: LOOPING

8/10/2015 18

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

3

12

Page 19: LOOPING

8/10/2015 19

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

3

True

12

Page 20: LOOPING

8/10/2015 20

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

3

123

Page 21: LOOPING

8/10/2015 21

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

4

123

Page 22: LOOPING

8/10/2015 22

int count;

for (count = 1; count <= 3; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

Output

count

Example (Cont ..)

4

False

123

When the loop control condition is evaluated and has value false, the loop is said to be “dissatisfied” and control passes to the statement following the for

structure.

Page 23: LOOPING

8/10/2015 23

int count;

for (count = 1;; count++)

{

printf(“%d\n”,count);

}

printf(“Count = %d\n”,count);

;

Example (Cont ..)

Output

count

4

123Count = 4

Page 24: LOOPING

8/10/2015 24

Points to Note about the Structure of the "for" Stmt "for" is lower case

the three parameters are surrounded by

parentheses

the three parameters are separated by semi-

colons

all three parameters must be identified (but

could be empty)

can initialize variable(s) within the

parentheses e.g., (int counter = 0; … )

can have multiple initialization and increment

statements (separated by commas)

increment expression may involve ++ or --

operators

no punctuation follows the closing parenthesis

Page 25: LOOPING

8/10/2015 25

Structure of a "while" Loop

while (boolean-expression)

statement-1

where statement-1 can be simple or compound

Page 26: LOOPING

8/10/2015 26

Structure of the "while" Statement

while ( count < 3 )

while starts with a lower case letter

the boolean expression is enclosed in

parentheses and can be simple or complicated

there is no punctuation that follows the

closing parenthesis (this is a common error)

there is no punctuation that follows the

closing brace

Page 27: LOOPING

8/10/2015 27

"while" with no block

Basic while (without a block)

int count = 0;

while ( count <= 3 )

printf(“%d\n”,count);

note: count ++ means count = count + 1

Page 28: LOOPING

8/10/2015 28

"while" with a Block

while with a block

int count = 3;

while ( --count >= 0 )

{

printf(“%d\n”,count);

}

Page 29: LOOPING

8/10/2015 29

Increment Using the Postfix Operator ++

Using ++

Example: count ++;

Means the same ascount = count + 1;

but uses value of count and then

increments

count --

similar to ++ but means count =

count - 1;

uses value of count and then

decrements

Page 30: LOOPING

8/10/2015 30

Increment Using the Prefix Operator ++

Example: ++count;

Means the same as

count = count + 1;

but the increment takes place before

the statement is executed

There is also a prefix -- operator

Example of the -- prefix operator:

amtOwed = --numEmpl * amtPerEmpl;

Page 31: LOOPING

8/10/2015 31

Clarifying Prefix and Postfix

What will be printed:

int i = j = 15;

prinf(“%d\n”,i);

prinf(“%d\n”,i++);

prinf(“%d\n”,i);

prinf(“%d\n”,j);

prinf(“%d\n”,++j);

prinf(“%d\n”,j);

Page 32: LOOPING

8/10/2015 32

int count;

count = 4; // initialize loop variable

while (count > 0) // test condition

{

printf(“%d\n”count); // repeated action

count--; // update loop variable

}

printf(“Done\n”);

Example

Page 33: LOOPING

33

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

Example (cont.)

Page 34: LOOPING

8/10/2015 34

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

4

Example (cont.)

Page 35: LOOPING

8/10/2015 35

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

4

True

Example (cont.)

Page 36: LOOPING

8/10/2015 36

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

4

4

Example (cont.)

Page 37: LOOPING

8/10/2015 37

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

3

4

Example (cont.)

Page 38: LOOPING

8/10/2015 38

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

3

4

True

Example (cont.)

Page 39: LOOPING

8/10/2015 39

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

;

Output

count

3

43

Example (cont.)

Page 40: LOOPING

8/10/2015 40

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

2

43

Example (cont.)

Page 41: LOOPING

8/10/2015 41

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

2

43

True

Example (cont.)

Page 42: LOOPING

8/10/2015 42

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

2

432

Example (cont.)

Page 43: LOOPING

8/10/2015 43

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

1

432

Example (cont.)

Page 44: LOOPING

8/10/2015 44

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

1

432

True

Example (cont.)

Page 45: LOOPING

8/10/2015 45

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

1

4321

Example (cont.)

Page 46: LOOPING

8/10/2015 46

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

4321

0

Example (cont.)

Page 47: LOOPING

8/10/2015 47

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

0

4321

False

Example (cont.)

Page 48: LOOPING

8/10/2015 48

int count;

count = 4;

while (count > 0)

{

printf(“%d\n”count);

count--;

}

printf(“Done\n”);

Output

count

0

4321Done

Example (cont.)

Page 49: LOOPING

8/10/2015 49

"do while" Loop sometimes called a "do" loop

"do while" loop has the test at the end

Example:

char response = 'Y';

do

{

… //other statements in body of loop

printf(“Edit more Y?N”);

scanf(“%c”,response);

} while (response == 'Y');

Relational expression follows word while

Note: no punctuation after do

Note: remember to put the ; following the while stmt

Page 50: LOOPING

8/10/2015 50

Example 2

// display the numbers from 4 to 1

// using the posttest do while loop

int count;

count = 4; // initialize loop variable

do

{

printf(“%d\n”,count); // repeated action

count--; // update loop variable

} while (count > 0); // test condition

printf(“Done\n”);

Page 51: LOOPING

8/10/2015 51

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

4

Page 52: LOOPING

8/10/2015 52

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

4

4

Page 53: LOOPING

8/10/2015 53

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

;

Output

count

3

4

Page 54: LOOPING

8/10/2015 54

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

3

4

Page 55: LOOPING

8/10/2015 55

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

3

4

True

Page 56: LOOPING

8/10/2015 56

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

3

43

Page 57: LOOPING

8/10/2015 57

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

2

43

Page 58: LOOPING

8/10/2015 58

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

2

43

Page 59: LOOPING

8/10/2015 59

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

2

43

True

Page 60: LOOPING

8/10/2015 60

Example 2

int count;

count = 4;

do

{

P rintf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

2

432

Page 61: LOOPING

8/10/2015 61

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

1

432

Page 62: LOOPING

8/10/2015 62

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

1

432

Page 63: LOOPING

8/10/2015 63

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

1

432

True

Page 64: LOOPING

8/10/2015 64

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

1

4321

Page 65: LOOPING

8/10/2015 65

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

0

4321

Page 66: LOOPING

8/10/2015 66

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

0

4321

Page 67: LOOPING

8/10/2015 67

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

0

4321

False

Page 68: LOOPING

8/10/2015 68

Example 2

int count;

count = 4;

do

{

printf(“%d\n”,count);

count--;

} while (count > 0);

printf(“Done\n”);

Output

count

0

4321Done

Page 69: LOOPING

8/10/2015 69

When Do You Use a "do while" Loop?

Very useful when one must ask the

question at the end of the loop

You use this when you know you want to

execute the body of the loop at least

once

Examples:

Do you want to enter another customer?

Entering a value to be edited. You enter

the value in the body of the loop, and then

after the body is completed, you check to

see if the value entered was acceptable.

Note that you must enter a value in order

to be able to have something to edit.

Page 70: LOOPING

8/10/2015 70

Comparing "for" and "while" Statements

for (init-expr-1; bool-expr-1; incr-expr-1)

{ statement-1 }

becomes

init-expr-1

while (bool-expr-1)

{

statement-1

incr-expr-1

}

Page 71: LOOPING

8/10/2015 71

Summary Writing loops gets easy with practice

Page 72: LOOPING

8/10/2015 72

Practice

Write a for loop that will add up the values

5 cubed plus 10 cubed plus … all the way up

to 350 cubed

Write a while loop that will read in an

integer value from the console, and verify

it is between 1 and 10. If not, it will

write an error message on the console, and

prompt the user to enter the value again.

Write a for loop that will calculate the

factorial of a number.