Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

11
Lecture 33: Loops (cont.) Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011

Transcript of Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Page 1: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Lecture 33: Loops (cont.)

Chapter 6: Sections 6.2, 6.3

Roxana GheorghiuFriday, April 1, 2011

Page 2: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Definition:

Do While condition statement1 statement2 …Loop

Example: Do While x<10 a=a+1

b=a+5x =x+2

Loop

Do Loop (review)

Page 3: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Definition:

Do statement1 statement2 …Loop Until condition

Example: Do a=a+1

b=a+5x =x+2

Loop Until x >10

Do Until (review)

Page 4: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

DefinitionFor var =initVal To finalVal

statement1statement2 …

NextExample

For x=1 to 5 a =a+1 b=a+5Next

For … Next

Page 5: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Used when the number of times the loop must be executes is known beforehand

The control variable is initialized and then automatically changes its value after each iteration of the loop

It is a PRETEST kind of loop like Do While Loop

The value of the control variable is incremented by 1 each step.

The value of the control variable should not be altered within the body of the loop

Continue For and Continue Do skips an iteration in a For and, respectively, Do loop

Exit For and Exit Do backs out of a For and, respectively, Do loop

For … next

Page 6: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

DefinitionFor var =initValue To finalValue Step s

statement1statement2

NextExampleFor i=5 To 10 Step 2

x =x+1Next

NOTE: step can have a negative value

For … Next (variation)

Page 7: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

1. Counters

2. Accumulators

3. Flags

4. Peek Method

Some technical information

Page 8: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

A numeric variable that keeps track of the numbers of items that have been processed

Used when we need to execute a loop only for a portion of data.

Ex: ◦ read the first 5 names from the file names.txt◦ determine how many different type of coins one

has

Counters

Page 9: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Is a numeric variables that total numbers

Used when we need to keep a total of all numbers read

Ex: add all numbers from a file and return this sum

Accumulator

Page 10: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Is a variable that keeps track of weather a certain situation has occurred

The data type most suitable for flags is the Boolean data type

Ex:

◦read all the numbers from a file. If you read a negative number make an error message box that says: “You shouldn’t have negative numbers in the file.”

Flags

Page 11: Chapter 6: Sections 6.2, 6.3 Roxana Gheorghiu Friday, April 1, 2011.

Used to check if we have reached the end of the file

Used with Do While and Do Until loops

This function return the ASCII value of the first character of the line about to be read

If the end of the file has been reached, this function returns -1

Peek Function