Small Basic - Example Loop
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!")
Else
TextWindow.WriteLine("Nope. Try again! Or not. You can remain wrong if you want.")
EndIf
EndWhile
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 https://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!
Small and Basically yours,
- Ninja Ed & Majed Marji
Comments
- Anonymous
January 31, 2016
Computers Today (part 1 of 6) blogs.msdn.com/.../computers-today.aspx ..... CS SPOTLIGHT: Girls in computer programming... why it matters!!! blogs.msdn.com/.../cs-spotlight-girls-in-computer-programming-why-it-matters.aspx ... Computational Thinking - Videos & Papers by Jeannette Wing blogs.msdn.com/.../computational-thinking-videos-amp-papers-by-jeannette-wing.aspx