Iteration or Repetition is the third programming construct that you need to know about. There are a number of ways of using iteration, but we shall start with the simplest, where you know how many times you want to repeat the loop.
Program's Aim: To display a birthday greeting message repeatedly for as many times as the years of your birthday.
Algorithm:
Reminder that Variables are containers into which we can place data of a specific form. Integers are whole numbers; strings are text.
| Form Text "Happy Birthday" | ![]() |
Label Name lblinstr Label Text - as shown |
|
Horizonatal Slider Name HScrollBar1 Largechange 1 Maximum 30 Minimum 1 SmallChange 1 |
|
Label Name lblAge text = blank |
|
| List Box Name lstGreeting | |
Button Name cmdGreeting Text Display Greeting |
|
Button Name cmdQuit Text End |
Note that in this case we are not using a Text Box to display our results but a List Box. Text boxes have limitations when you want to display repeating lines and where as List Boxes are usually used for displaying drop down lists they are ideal for displaying repeating lines. A scroll bar automatically appears when needed.
There are three procedures in this program including the usual quit procedure.
The first is the slider - as soon as we click on the slider it is activitated and the value will be displayed in the label. We allocate a variable to this value called "age" as we want to use the value in another procedure where we have the loop to display the greeting "age" number of times.
When we want a variable to be available across a number of procedures we have to declare it right at the top of our code page. Instead of a Dim statement we use Public as shown below.
CODE
Public Class Form1
Public age As Integer
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
age = HScrollBar1.Value
lblage.Text = age 'set the variable age to the value of the scroll bar.
End Sub
End Class
Private Sub cmdGreeting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGreeting.Click
Dim n As Integer ' n is the counter variable
Dim greeting As String ' greeting is the message displayed
greeting = "Happy Birthday!"
For n = 1 To age ' loop begins here
lstGreeting.Items.Add(greeting)
Next n
End Sub
Note how the List box is used above. If you want to have another go without a total restart then you must "clear" the list box otherwise your next iteration just gets added. In the code above in the line before n = 1 etc add lstGreeting.Items.Clear. This will clear the box before you start.
Programming Challenge - Simple Times Table Display:
Create a times table program which allows you to choose a number by using a slider bar (between 1 & 12) and display the times table in a list box. See below for hints:
The format of the display should be
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
etc
This should be written up properly with a statement of what the program should do; a sketch of your form and an Algorithm.
Copy and paste the finished code into your planning document. Include a screen capture of your form when working (alt - print screen then edit - paste into Word)
Using List Boxes: When running, your program should display 1 x 3 = 3 etc in your list box.line of the format
where 1 is a variable (say "n" a counter with values of 1 to 12)
X is text to represent the multiply symbol.
3 is the times table you are doing (say "mulitplier" - value comes from scroll bar)
= is text
and 3 is the answer when you multiple 1 x 3 (Say a variable called "answer")
So you will need to declare your variables - multiplier is global
A line of code to perform the calculation answer = i * multiplier
and your list box to display the result. We use an & to link the various bits together. The line should be
lstResult.Items.Add(i & " X " & multiplier & " = " & answer)
Note that the text has to go in " " whilst variables are used as is.