Iteration
Using loops

 in VB.NET



              1
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”!

                                             2
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 execute
  Next value

                                            3
For...Next example




                     4
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 loop
Syntax:
   While condition = true
        Line of code execute
   End While
                                        5
While…End While example




                          6
Conditional Loop Structures
Use Do…. Loop to run until a condition
becomes true
This 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 execute
   Loop Until condition = true

                                                7
Do… Loop Until example




                         8
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


                                                   9

Using loops

  • 1.
  • 2.
    Loop structures These areused 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”! 2
  • 3.
    Fixed Loop structures Ifwe 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 execute Next value 3
  • 4.
  • 5.
    Conditional Loop Structures Ifwe want the loop to repeat until a condition changes (at runtime). Use While …. End While to check the condition before running loop Syntax: While condition = true Line of code execute End While 5
  • 6.
  • 7.
    Conditional Loop Structures UseDo…. Loop to run until a condition becomes true This 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 execute Loop Until condition = true 7
  • 8.
  • 9.
    Programming terms When weneed 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 9