Using loops
-
Upload
nickywalters -
Category
Documents
-
view
3.752 -
download
0
Transcript of Using loops
1
IterationUsing loops
in VB.NET
22
Loop structures
These are used when we want lines of code to be executed many times
We might want it to run a set number of times.
We may need to check if various conditions have been met.
There are loops for all occasions and programmers have their “favourites”!
33
Fixed Loop structures
If we want the loop to run a fixed number of times, we use a For…..Next loop
This will use a variable as a counter
Syntax:For first value To last value
Line of code to executeNext value
44
For...Next example
55
Conditional Loop Structures
If we want the loop to repeat until a condition changes (at runtime).
Use While …. End While to check the condition before running loopSyntax:
While condition = true
Line of code executeEnd While
66
While…End While example
77
Conditional Loop Structures
Use Do…. Loop to run until a condition becomes trueThis code will run at least once Beware! It is possible to create loops that
carry on to infinity. Press Ctrl + Break to stop, but you may lose some unsaved work
Syntax:Do
Line of code to executeLoop Until condition = true
88
Do… Loop Until example
99
Programming terms
When we need to repeat a set of instructions,
we have 3 options: For….Next - runs for fixed number of loops
While…End While - runs while condition is true
Do…Loop Until - runs until condition becomes
true