Download presentation
Presentation is loading. Please wait.
1
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3
2
We use loops because computers are really good at doing the same thing over & over & over again...
3
There are three types of loops in Visual Basic While –entrance controlled Until –exit controlled For … Next –counting
4
How they are alike They all have the format: loop control loop body loop control They all cause the statements in the loop body to be executed repeatedly loop control contains the condition for loop termination
5
But… each type of loop has its own special features Sometimes it’s appropriate - even correct - to use one type over the others Often, the choice is yours Let’s see how they compare with each other...
6
While loops (Sec. 6.1) Do While condition loop body Loop The condition is checked before the loop body is executed. –It must be initialized outside the loop. The loop body must contain an instruction that affects the condition. –Otherwise, the loop will be infinite!
7
Until loops (Sec. 6.1) Do loop body Loop Until condition The condition is checked after the loop body is executed. –It does not have to initialized outside the loop The loop body must contain an instruction that affects the condition. –Otherwise, the loop will be infinite!
8
For … Next loops (Sec. 6.3) For i = m To n Step s loop body Next i Before the first iteration, i is initialized to m If m <= n (typical): –After each iteration, i = i + s –The loop continues to iterate as long as i is n, the loop terminates
9
When to use one or the other? If you don’t want the loop body to be executed even once before the condition is checked… If you want the loop body to be executed at least once before the condition is ever checked… If you know how many times the loop body should be executed...
10
Examples p. 251 - Ex 1 - while & for..next p. 251 - Ex 2 - while p. 253 - Ex 3 - until p. 253 - Ex 4 - while p. 259 - Problem 24 - until & while p. 278 - Ex 1 - for…next p. 279 - Ex 2, Ex 3 - for…next
11
Assignment Lab today –pg 259 # 25, 28 Homework due Tuesday: –Schneider – pg 260 # 32, 34 pg 285 # 20, 23
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.