Worksheet 4 - Loops

This worksheet covers three sample applications which illustrate the use of loops (For...Next, For Each....Next and Do....Loop While).  Watch the videos to see how the applications should work then pause the video when you get to "Now Build It" and have a go at building it before you watch the walk through of how the code is written.

Note that the video player has a "make full screen" button in the bottom right.  You'll want to use this when the video starts showing you the code walk through.


Example 1

A simple application which uses a loop to go through a string and produce a backward version of it.



Example 2

This is a slightly more complicated example which counts up the letters, digits and punctuation characters in a string using the Char.IsLetter, Char.IsDigit and Char.IsPunctuation function calls.  This also uses the For Each....Next loop which can be more elegant than the simple For...Next loop used in example 1.

 

 


Example 3

This final example is quite a lot more complicated than the other two and uses a Do...Loop While loop to put text entered into alphabetic order.  It also uses an array of Char characters to store and order the string.  We'll come back to arrays in more detail a future worksheet.

 

If you get through both these examples and are still keen to try more why not try the following:

  • Rewrite example 1 using a For Each loop.
  • Expand example 2 to count characters which aren't letters, digits or punctuation.