Small Basic - Tutorial

3.Conditions & Loops

Now our programs become intelligent thanks to the concept of conditional logic.

Conditional logic is when we evalutate whether an expression is True or False. Take for example a night club. When we ask for the users age we need to determine if the user is 18 years or over.

TextWindow.Write("Enter your age: ")
userAge = TextWindow.ReadNumber()

If userAge >= 18 Then
  TextWindow.WriteLine("Old enough to enter")
  TextWindow.WriteLine("Sorry too young")

We test the expression userAge >= 18 and if it's true then the person is old enough. Else it's false then the person is too young.


What about a count down from 10 to 0. While count > 0 the while condition is true so the loop keeps looping. The While loop is broken when the condition becomes false.

count = 10

While count > 0
  count = count - 1

The For loop is good when you know how many times you wish to loop.

For i = 1 To 10

For loops can use the Step keyword as well. In this example the loop iterates in Steps of -1.

For i = 10 To 1 Step -1

Loops are a type of branching. Instead the program being read 1 line after the other, the loop causes the program to branch back to the head of the loop until the loop is broken.

There are also nested loops, that's when you have a loop inside a loop. I discuss these later.