error trapping, do/while loops, loop errors

31
# 1 Error Trapping, Do/While Loops, Loop Errors How do we repeat lines of code over and over? What is an “infinite loop”? What is an “off-by-one” error? What is so dangerous about putting the condition at the bottom?

Upload: juliet

Post on 06-Jan-2016

60 views

Category:

Documents


4 download

DESCRIPTION

Error Trapping, Do/While Loops, Loop Errors. How do we repeat lines of code over and over? What is an “infinite loop”? What is an “off-by-one” error? What is so dangerous about putting the condition at the bottom?. How to make a random number appear in a cell in Excel:. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Error Trapping, Do/While Loops, Loop Errors

# 1

Error Trapping, Do/While Loops, Loop Errors

Error Trapping, Do/While Loops, Loop Errors

How do we repeat lines of code over and over?

What is an “infinite loop”?

What is an “off-by-one” error?

What is so dangerous about putting the condition at the bottom?

Page 2: Error Trapping, Do/While Loops, Loop Errors

# 2CS 105 Spring 2010

How to make a random number appear in a cell in Excel:

Private Sub cmdRandom_Click()

Cells(1,1).Value = Rnd

End Sub

• This code places a random number in cell A1.

• The function Rnd returns a random number between 0 and the upper limit specified. If you don’t put in an upper limit, VB assumes it is .9999 but stops short of one!

• Note: Every time it is called, it should generate a different number.

Page 3: Error Trapping, Do/While Loops, Loop Errors

# 3CS 105 Spring 2010

Filling many cells with the same random number

Private Sub cmdFillRange_Click()

End Sub

• So now how can we put a different random number in each cell in a range?

Page 4: Error Trapping, Do/While Loops, Loop Errors

# 4CS 105 Spring 2010

Filling many cells with a unique random number for each

Private Sub cmdFillRange_Click() Cells(1,1)Value = Rnd Cells(2,1)Value = Rnd Cells(3,1)Value = Rnd Cells(4,1)Value = Rnd

End Sub

• This code places a different random number in every cell in a range.

• So now how can we put a different random number in each cell in a range without tediously writing each one?

Page 5: Error Trapping, Do/While Loops, Loop Errors

# 5CS 105 Spring 2010

This loop places a random number in a cell then moves to the next cell, where it does the same thing

intCounter = 1

Do While intCounter < 11 Cells(intCounter, 1).Value = Rnd

intCounter = intCounter + 1

Loop

Do Loop

Page 6: Error Trapping, Do/While Loops, Loop Errors

# 6CS 105 Spring 2010

The “loop index” or counter

• Each trip through the Body of the Loop is called an Iteration of the Loop.

• The counter is a variable that is growing with each iteration.

• If the counter does not exceed the Final Value, statements in the loop body are executed.

Page 7: Error Trapping, Do/While Loops, Loop Errors

# 7CS 105 Spring 2010

How many cells will display a number?

Private Sub cmdRandom_Click()Dim intCounter as IntegerintCounter = 1

Do While intCounter < 11 Cells(intCounter, 1).Value = Rnd

intCounter = intCounter + 1

Loop

Page 8: Error Trapping, Do/While Loops, Loop Errors

# 8CS 105 Spring 2010

Process Explained

The counter starts at 1, so the iteration of the loop looks like this:

Cells(intCounter, 1).Value = RndCells(1,1).Value = Rnd

The code adds 1 to the counter. So the next time through,

Cells(2,1).Value = Rnd

Page 9: Error Trapping, Do/While Loops, Loop Errors

# 9CS 105 Spring 2010

The Do Loop The Do Loop

• NOTE: Sometimes you don't want to repeat the body of a loop a predetermined number of times

• You can continue to repeat the body of a loop while some condition continues to be true, or until some condition becomes true.

• (a password, for example)

• You can test the condition at the beginning of each iteration and/or at the end of each iteration.

Page 10: Error Trapping, Do/While Loops, Loop Errors

# 10CS 105 Spring 2010

Flowchart of a Do/While Loop

• 1st, check to see if loop should start

• If so, execute code

• Then test again• And so on…

Initialize starting condition

Is loop condition

true?

Execute statementsin loop

End

TRUE

FALSE

Start

Page 11: Error Trapping, Do/While Loops, Loop Errors

# 11CS 105 Spring 2010

Loops Make Code Repeat

Page 12: Error Trapping, Do/While Loops, Loop Errors

# 12CS 105 Spring 2010

Examples of Do Loops (without # of times) Examples of Do Loops (without # of times)

Do While [Logical Condition] Add one to running total

Loop

Do Until [Logical Condition] Add one to running total

Loop

Do Add one to running total

Loop While [Logical Condition]

DoAdd one to running total

Loop Until [Logical Condition]

Page 13: Error Trapping, Do/While Loops, Loop Errors

# 13CS 105 Spring 2010

Do While Loop

‘we use the variant data type with IsNumeric

Private Sub cmdWage_Click() Dim vntWage as Variant vntWage = InputBox("Enter Wage", "Wage", 1000) Do While IsNumeric(vntWage) = False

MsgBox "Wage must be a number”vntWage = InputBox("Enter Wage", "Wage",

1000) Loop

End Sub

Page 14: Error Trapping, Do/While Loops, Loop Errors

# 14CS 105 Spring 2010

Do Until Loop

Dim vntWage as Variant vntWage = InputBox("Enter Wage", "Wage", 1000)

Do Until IsNumeric(vntWage) = True

MsgBox "Wage must be a number”vntWage = InputBox("Enter Wage", "Wage",

1000)

Loop

Page 15: Error Trapping, Do/While Loops, Loop Errors

# 15CS 105 Spring 2010

Do Until Loop with Counter

Dim vntWage as VariantDim intCounter as Integer

vntWage = InputBox("Enter Wage", "Wage", 1000)

Do Until IsNumeric(vntWage) = True Or intCounter > 2

MsgBox "Wage must be a number”vntWage = InputBox("Enter Wage", "Wage",

1000)intCounter = intCounter + 1

Loop

Page 16: Error Trapping, Do/While Loops, Loop Errors

# 16CS 105 Spring 2010

Debugging

Step through the procedure

Debug toolbar

Immediate window

Locals window

Page 17: Error Trapping, Do/While Loops, Loop Errors

# 17CS 105 Spring 2010

Debugging, cont.

• What do these terms mean?

–Breakpoint–Step into–Run-time error

Page 18: Error Trapping, Do/While Loops, Loop Errors

# 18CS 105 Spring 2010

Watching values grow

First iteration:intTotal = 0intIndex = 1 Do While intIndex < 4 intTotal = intTotal + intIndex intIndex = intIndex + 1Loop

Second iteration:

Do While intIndex < 4 intTotal = intTotal + intIndex intIndex = intIndex + 1Loop

12

110

2

23

1 2

3

intTotal= 1intIndex= 2Iterations= 1

intTotal= 3intIndex= 3Iterations= 2

Page 19: Error Trapping, Do/While Loops, Loop Errors

# 19CS 105 Spring 2010

Working it through

Dim intIndex As IntegerDim intTotal As Integer intIndex = 1 Do While intTotal < 4

intTotal = intTotal + intIndex intIndex = intIndex + 1 Loopis equivalent to

intTotal = intTotal + 1 intTotal = intTotal + 2

intTotal = intTotal + 3

Page 20: Error Trapping, Do/While Loops, Loop Errors

# 20CS 105 Spring 2010

Working it through

Dim intIndex As Integer Dim intTotal As Integer intTotal = 0 intIndex = 1

Do While intIndex < 4 intTotal = intTotal + intIndex intIndex = intIndex + 1

Loop

1 = 0 + 1 After the first interation, 3 = 1 + 2 intTotal is 1, then 3,

6 = 3 + 3 and finally 6. intIndex is 4 when the code stops. The loop had 3 iterations, when intIndex was 1, 2, and 3

Page 21: Error Trapping, Do/While Loops, Loop Errors

# 21

What is a logic error in coding?

• A. Code that does not run

• B. Code that runs, but does not get the result that what we wanted!!

CS 105 Spring 2010

Page 22: Error Trapping, Do/While Loops, Loop Errors

# 22CS 105 Spring 2010

Errors – off by one Errors – off by one

Loops are prone to logic errors.• Off-by-one errors

–one too few loop passes (e.g. < was used instead of <= )

–one too many loop passes (e.g. <= was used instead of < )

Page 23: Error Trapping, Do/While Loops, Loop Errors

# 23CS 105 Spring 2010

Example -- off by one

vntGuess = 0intNumTries =3Do While intNumTries > 1 vntGuess=InputBox(“Enter your guess”) Cells(intNumTries, 2).Value = vntGuessintNumTries = intNumTries – 1‘add If statement to check guessLoop

If you wanted 3 tries, you goofed!

Page 24: Error Trapping, Do/While Loops, Loop Errors

# 24CS 105 Spring 2010

Errors -- Never executes

intNumTries = 2Do While intNumTries >= 3 Range(“A2”).Value = RndLoop 2> 3 is FALSE , so the

loop never executes—the loop has no iterations

Page 25: Error Trapping, Do/While Loops, Loop Errors

# 25CS 105 Spring 2010

Errors – commands in wrong order

Dim intCounter as IntegerintCounter = 1 ‘why do we make it

one?

Do Until intCounter > 100 Cells( intCounter, 1).Value = intCounter *

2 intCounter = intCounter + 1 Cells(intCounter, 1).Interior.ColorIndex =

6Loop

When did you change intCounter? The first cell will not have a changed background color.

Page 26: Error Trapping, Do/While Loops, Loop Errors

# 26CS 105 Spring 2010

Errors – Termination problems,

'infinite' loops

Errors – Termination problems,

'infinite' loops • Loop termination - Some statement in

the body of the loop must affect the test condition and cause termination.

• Use Ctrl + Break to get out!

• If the

loop termination condition is never reached

it becomes an infinite loop

Page 27: Error Trapping, Do/While Loops, Loop Errors

# 27CS 105 Spring 2010

Yes, you get an Infinite Loop

Page 28: Error Trapping, Do/While Loops, Loop Errors

# 28CS 105 Spring 2010

Example 'infinite' loop

intCounter = 1

Do Until intCounter > 5 Cells( intCounter, 1).Value = intCounter

*2Loop

intCounter = intCounter + 1

Page 29: Error Trapping, Do/While Loops, Loop Errors

# 29CS 105 Spring 2010

Checking condition at the bottom of the loop

Dim intCounter as IntegerintCounter = 1 ‘why do we make it

one?

Dim intCounter as IntegerintCounter = 1Do

Cells(intCounter, 1).Value = intCounter *2

intCounter = intCounter + 1

Loop Until intCounter > 2

‘how many times does this loop execute?

Page 30: Error Trapping, Do/While Loops, Loop Errors

# 30CS 105 Spring 2010

Do Loop While (could be “fatal”) loop error

Const mstrSECRETWORD as String = “dropitnow”

Private Sub cmdDropAtomBomb_Click()

Dim intRetries As IntegerDim strTry As StringintRetries = 2

Do strTry = InputBox("Enter the password and click OK")

intRetries = intRetries - 1

DropBomb ‘ Call Sub to Drop the Bomb

Loop While strTry <> mstrSECRETWORD AND intRetries >1

Page 31: Error Trapping, Do/While Loops, Loop Errors

# 31CS 105 Spring 2010

To Summarize:

• Loops can be based on conditions > = < <>

• What is an “infinite loop”?

• What is an “off-by-one” error?

• What is so dangerous about putting the condition at the bottom?