Spot the Loop, Page 2
Don't look at me, but there's a loop on the loose. In this section, we're going to look at two things - firstly, how to create a "loop" in code and secondly, how to get information from the user.
Karl explain loops!
Loops are amazingly useful. For instance, you may want to display a message box for every single student in your highly successful grammar school. You can do this two ways - either manually code every single message box (highly inefficient, goes against all coding practices and not very tech-savvy) or use a loop that says, in English, "display a message box for every student".
Creating a loop is pretty simple. Let's look at a sample piece of code
For i = 1 to 5Msgbox "I have looped round " & i & " times"Next i
I'll explain what's happening here.
The first line tells Visual Basic how many times it should loop around. It's saying - loop around this bit of code five times and store the number of times it has looped around in "i". The second line is just a simple message box we display that shows "i" - don't forget, "i" is just the number of times it has looped around. The third line, "Next i" just tells it to go back to the beginning until it's gone from one to five.
Phew! That's a lot to take in. But don't worry - you can do it!
Just think of what you could use this for! You could say to Visual Basic - loop through all the students in our school database and add them to a box on the screen.