Sunday, 6 October 2013

VB Lesson 6: The For... Next Loop

So this is the final lesson that I did in bulk, so after this hopefully every post will be a lot more together and coherent. Nothing will overlap in a fuzzy way, it'll only overlap when I want it to. Not to mention I'll be on the same page as everyone else. Plus, my head won't hurt anymore, like it did after three solid hours of cramming the first 6 lessons of Computing in.

Visual Basic Lesson 6:

So we have covered two types of loops, let's move onto a third. This is another iteration statement and it does repeat a selected set of instructions, the difference is that it repeats for a set number of times. It is unlike the repeat loop, which repeats as many times until a condition is met, as it just repeats for the number of times you specify.

Here is an example of a program which will take any number and give you the first 12 from its times table, I made it out of a program which was originally just for the 12 times table. This is also an example of program which displays the output in a text box rather than a MsgBox:


The reason it is an example of a loop is because it does the calculation specified 12 times before it stops.

Here is a code which displays another use of the for next loop:


This coding shows that the loop can be used to count down from one to value until it reaches another specified value, but also shows off the fact that you don't have to go down in intervals of one. You can choose to go down (or up) in steps of however many you like (in this case it's steps of minus 5 and is therefore counting down) you're not particularly limited.

A final example of a for next loop code is this:



What this code shows is similar to the times table program, except this is specifically for one thing (displaying the square numbers from 1 to 100 in a text box.) not for any times table. It is once again showing off the fact that this loop is for a specified number of times, before it stops and moves on.

And that is the end of another iteration statement, sorry it is not too great, I have written 5 (technically 6) of these in a row and so I have lost creativity and will. Hopefully future ones will be a lot better!!

See you soon Lalin :D.

No comments:

Post a Comment