25. Repetition

The last major programming construct we need to learn about at this introductory level of programming is “Repetition“. In the previous lesson I left a comment for what I referred to as “SPECIAL CODE” that was going to be dealt with in this lesson. Well the only reason I’ve labelled it “special” is because it’s not necessary for our game but it does ensure we’ve addressed all the topics you should know about to get you on the right track when it comes to programming.

Consider the following example of what we might want our program to do when the player has won:

The only thing to really take note of here is the Instructions box which is to list the fact they’ve won, 10 times. You may have already worked out a way to get this to happen, i.e:

lblInstructions.Text = "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"
lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                       "Congratulations, you win!"

As you can see though, this is cumbersome and repetitive. What if I wanted to write that message out 1000 times…then you would really see repetitive code! This is where a loop can help us. Why don’t we start by looking at a flowchart showing a neater solution than our one above:

Here you can see a neat solution that would allow us to add the “Congrats” message 10,000 times if we wanted with one very minor change of our flowchart (and subsequent code). Now let’s see how this can be done in Visual Basic.

The “While” loop
When attempting to implement some form of repetition, the “While” loop is one of the better ways to do so. It will work for all types of repetition you might be trying to create and it’s very similar to an “if” statement which makes it easy to learn. Here’s the VB code that matches our flowchart above:

lblInstructions.Text = "Congratulations, you win!"
counter = 1
While (counter < 10)
   lblInstructions.Text = lblInstructions.Text + vbNewLine + _
                          "Congratulations, you win!"
   counter = counter + 1
End While

Here you can see the “While” and “End While” lines wrap some code very much like an “if” statement did, the only difference is that if the Boolean Expression is evaluated to be true, then when the computer has executed the code inside the “While” loop, it jumps back to the top of the loop to test the Boolean Expression again, whenever this Boolean Expression becomes false, the loop is finished…simple!

Relational Operators
You may be curious about the use of a less than sign ( < ). This is simply another method of comparing values. In our “if” statements, we were using the equals sign but you are allowed to compare values with any Relational Operator, the available operators are:

  •  (x = y) : “x” equals “y”
  • (x <> y) : “x” does not equal “y”
  •  (x < y) : “x” is less than “y”
  • (x <= y) : “x” is less than or equal to “y”
  •  (x > y) : “x” is greater than “y”
  • (x >= y) : “x” is greater than or equal to “y”

The only element now left unexplained is where the “counter” variable came from? You should get used to using variables and not fear their introduction in your code. You simply won’t be able to visualise all necessary variables before you start coding so adding them as you go is perfectly acceptable. So where will this one be defined and what sort of data type will it be? These are questions you should be able to answer now.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>