For...Next Loops

Report
CS0004: Introduction to
Programming
Repetition – For Next Loops
Review

A loop is used to…


Each repetition of the loop is called


Pretest and Posttest.
Do While loops repeat until the condition is…


a pass, or iteration.
Do Loops come in two forms:


repeatedly execute a sequence of statements a number of
times.
False
Do Until loops repeat until the condition is…

True
Review
General Form of Pretest:
Do While condition
statement(s)
Loop

General Form of Posttest:
Do
statement(s)
Loop While condition

For…Next Loops
When we know exactly how many times a loop should
be executed, a special type of loop, a For…Next loop, can
be used.
 For instance…
For i As Integer = 1 To 5
MessageBox.show(i)
Next

…will show the numbers 1 through 5 in message boxes.
For…Next Loops
We could write the same program with a Do loop
Dim i As Integer = 1
Do While I <= 5
MessageBox.show(i)
i += 1
Loop


However, this is more verbose and less intuitive
For…Next Loops
General Form:
For i As numberDataType = m To n
statement(s)
Next
 i can be any valid variable name (i is fine here though)






i is a counter variable that is incremented (one is added to it) after
every iteration of the loop.
numberDataType is any number data type (most often it is
Integer)
m is the initial value that i takes (i is initialized to m)
The loop runs until i > n. n is called the terminating value.
i does not need to be declared beforehand, because the loop
for all intents and purposes declares it. It has block-level
scope.
For…Next Loops
General Form:
For i As numberDataType = m To n
statement(s)
Next
 When a program reaches a For…Next Loop:
1. The counter variable receives the initial value
2. It checks to see if the counter variable is greater than
the terminating value

If it is, the programs jumps out of the loop
If it is not, the statement(s) inside of the loop execute
A.
B.
a.
b.
The counter variable is incremented
Goes back to step 2.
For…Next Loops
Set counter
variable to initial
value
Is counter
variable >
terminating
value
No
Execute
statements
within loop
Increment
counter variable
Execute
statements
within loop
Yes
Counter Variables Declared Outside of Loop
You can declare the counter variable outside of the loop
if you want to use it outside of the loop:
Dim i As Integer
For i = 1 To 5
MessageBox.show(i)
Next

Does the same thing as…
For i As Integer = 1 To 5
MessageBox.show(i)
Next
For…Next Example 1

New Topics:

For…Next Loop
Step Values
By default For…Next loops increment the counter variable by
1 every iteration of the loop.
 You can also define how much is added to the counter variable
every iteration. This amount is called a step value.
For i As Integer = 1 To 11 step 5
MessageBox.show(i)
Next
 This loop will display 1, 6, and then 11 in separate dialog boxes.
 General Form:
For i As numberDataType = m To n Step s
statement(s)
Next
 Where s is the step value.

For…Next Loop Examples 2, 3, 4

New Topic:



Step Value
Negative initial values, and step values
Nested For Loops
For…Next Notes:

If you want to skip an iteration of a For…Next Loop you can
use the Continue For statement (there is also a
Continue Do statement)




When Continue For is encountered, the loop then skips to the
next iteration without executing the code below it in the current
iteration.
Likewise, much like a Exit Do, there is also an Exit For.
Again, counter variables have block-level scope in the for loop
they are used in.
Any decision statements or repetition statements are followed
by statement(s). These are called blocks. Any blocks can
be nested in any other blocks (If statements can be nested in
for loops, etc.)
Type Inference
As we write our programs now, this statement is legal:
Dim var = 1
 Question: How does the compiler know what type var is?
 Answer: It infers the type from the value it is initially given.
 The compiler gives var the type Integer because 1 is of type
Integer.
 This can be dangerous for beginning programmers:
Dim var = 2
var = 2 * 4.5
 This will result in an error, because even though we may have
wanted var to be of type Double, but we did not control what
type it was given initially, and it was inferred to by of type
Integer.
 To disallow this type inference (implicit typing, or duck typing) add
Option Infer Off to the top of the program.


similar documents