Using loops

9
1 Iteration Using loops in VB.NET

Transcript of Using loops

Page 1: Using loops

1

IterationUsing loops

in VB.NET

Page 2: Using loops

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

Page 3: Using loops

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

Page 4: Using loops

44

For...Next example

Page 5: Using loops

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

Page 6: Using loops

66

While…End While example

Page 7: Using loops

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

Page 8: Using loops

88

Do… Loop Until example

Page 9: Using loops

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