Sunday, 6 October 2013

VB Lesson 5: Iteration (Do Loops)

So after the confusion of lesson 4 it is nice to have something which I sort of understand. This was a topic which wasn't covered by legendary Lalin, it was covered by the lovely Clive instead. So I came under a different teaching style here. However, hopefully my coverage will be just a thorough though. So, onto the lesson :D.

Visual Basic Lesson 5:

Here is another type of statement which we haven't come across before, iteration statements, sounds fancy but really isn't hard to figure out. Essentially they are statements which make instructions, coded by you, to repeat or loop many times. There are different types of loops, repeat loops, while loops and for next loops, however we shall only be looking at repeat and while loops today, for next loops are covered in lesson 6.

Let's start with the repeat loop. Basically, what a repeat loop does is loop a section of code until the condition set becomes true. There is no set number of times, it just continues until the condition is met. It is useful for data validation.

An example for data validation would be:

Dim age As Integer

Do
age = InputBox("How old are you?")
If age >= 100 Then
MsgBox("Must be less than 100!")
End if
Loop until age<100

This code ensures that valid data must be entered, before moving on from that section of code. The code would continue to loop until an age below 100 is entered and the program would then move onto the next part of the code.

Another example for the use of a repeat loop would be:

Dim total as Integer = 0
count = 0
Dim marks As Integer

Do
Number = InputBox("Enter marks")
total = total + marks
count = count + 1
Loop until count = 5

MsgBox("Total" & total)

This code ensure that this section of code will loop 5 times, before moving onto the next section of code. It is useful for entering multiple marks and gaining a total. However, similar coding can mean that you can get several pieces of data entered without having to repeat the whole program.

A final example of a repeat loop is:

Dim total As Integer = 0

Do
Number = InputBox("Enter number")
total = total + number
Loop until number = 0

What would happen in this program would be the InputBox continues to come up, until the user enters 0. That means that several pieces of data are able to be added and when the user has finished, they simply enter 0 and the program stops looping. 0 in this case is a rogue value. Why? Because 0 isn't real data that the user wants to enter, it's just a value which has been assigned to the code in order to make the loop stop.

This now brings us to the while loop. So, the while loop continues using a particular piece of coding whilst the condition is true. As soon as the condition becomes false, the loop stops and the program moves on.

For example:

Dim reply as string

reply = InputBox ("Run program?")
Do while reply = "Yes"
reply = InputBox ("Another go?")
Loop

This program means that if the user types "yes" the program runs, but if the user types "no" then the program doesn't even run once. This cannot be done with a repeat loop.

And that's pretty much repeat loops and while loops, there's not much to them. They're both exceptionally useful when a piece of coding needs to be used more than once and they ensure self-documenting code as it they mean that the code is kept neat and as concise as possible, there's no need to keep copy and pasting!

So goodbye until next time, where for next loops will be tackled... Yay :D.






No comments:

Post a Comment