Transcript For...Next Loops
CS0004: Introduction to Programming
Repetition – For Next Loops
A loop is used to… repeatedly execute a sequence of statements a number of times.
Each repetition of the loop is called a pass, or iteration.
Do Loops come in two forms: Pretest and Posttest.
Do While loops repeat until the condition is… False Do Until loops repeat until the condition is… True
General Form of Pretest: Do While
Loop General Form of Posttest: Do
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.
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
General Form: For
can be any valid variable name (
is a counter variable that is incremented (one is added to it) after every iteration of the loop.
Integer ) is fine here though) is any number data type (most often it is
is the initial value that
is initialized to
) The loop runs until
is called the terminating value.
does not need to be declared beforehand, because the loop for all intents and purposes declares it. It has block-level scope.
General Form: For
When a program reaches a For…Next Loop: A.
The counter variable receives the initial value It checks to see if the counter variable is greater than the terminating value a.
If it is, the programs jumps out of the loop If it is not, the statement(s) inside of the loop execute The counter variable is incremented Goes back to step 2.
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
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
is the step value.
For…Next Loop Examples 2, 3, 4
New Topic: Step Value Negative initial values, and step values Nested For Loops
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 iteration.
is encountered, the loop then skips to the next iteration without executing the code below it in the current 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) for loops, etc.) . These are called blocks. Any blocks can be nested in any other blocks (If statements can be nested in
As we write our programs now, this statement is legal: Dim var = 1 Question: How does the compiler know what type var This can be dangerous for beginning programmers: Dim var = 2 is?
Answer: It infers the type from the value it is initially given.
The compiler gives Integer .
var the type Integer because 1 is of type var = 2 * 4.5
This will result in an error, because even though we may have wanted var Integer .
to be of type Double , but we did not control what type it was given initially, and it was inferred to by of type To disallow this type inference (implicit typing, or duck typing) add Option Infer Off to the top of the program.