Let’s try a basic example. Type in this code:
While (ans <> 13) ‘Until the user answers 13
TextWindow.Write(“Whats 7 + 6? “)
ans = TextWindow.ReadNumber()
If (ans = 13) Then ‘User answered correctly
TextWindow.WriteLine(“You got it!”)
TextWindow.WriteLine(“Nope. Try again! Or not. You can remain wrong if you want.”)
This loop keeps going until your user answers 13! If she answers correctly it tells her. Otherwise, it displays, “Nope. Try again! Or not. You can remain wrong if you want.” And then it goes back to the top to keep looping until she types in 13.
The general form of the While loop looks like this:
While (condition) ‘While the test conditions true,
Statement(s) ‘run these statements (loop body),
EndWhile ‘and then go back to check again.
Note: Although it’s not required by Small Basic, we used parentheses around the While loop’s condition (ans <> 13), and we indented the body of the While loop. This makes the programs easier to read!
Head to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!
Small and Basically yours,
– Ninja Ed & Majed Marji