Which Flavour, Sir?, Page 3
I bet you thought that was it with loops, eh? No such luck.
So you thought there was only one type of loop, eh?
Loops come in many different flavours. The one I just demonstrated was a pretty simple (Reader: What!?) loop - you tell it how many times to do something - and it does it!
However they can get a little more complicated. You may want to keep asking a user for a student name until they type in "END" or something.
We can do that with a "Do Until" loop. Let's take a peek at some sample code-
Dim UserInput as StringDo Until UserInput = "END"UserInput = InputBox("Enter the student name, or type END to finish:")Msgbox "You entered student - " & UserInputLoop
OK, you're probably wondering what I'm blabbling on about here. Don't worry - I tend to get all geeky every now and then. Just humour me.
The first line merely declares a variable called "UserInput" to hold a string (aka plain text). Remember we talked about variables in the last tutorial? We're just telling VB we want to store some text in a thing called "UserInput".
The second line tells Visual Basic to loop around until the "UserInput" variable is equal to "END".
The third line introduces a really neat function weve not talked about yet. It's called the InputBox and is just a cool way of getting information from the user.
When you run this, a little box will appear for the user to enter some information - in this case, a student name. Whatever they enter will be assigned to "UserInput".
Once again, we're just saying the UserInput variable should equal whatever the user puts in that InputBox.
And the fourth line just says- loop back to the start again. And at the start, Visual Basic asks the question once more - does UserInput equal "END"? If it does, then I'm going to stop looping around. If not - I'm going to carry on!
Get it? Got it! Good!
Have you noticed the bug yet? When you enter "END" in the InputBox, it displays the message "You entered student - END". If you're feeling confident, try writing an If-Then statement as discussed in previous tutorials to avoid this occurring.
Page 3 of 7