Small Basic #3: Keywords

The For, To, EndFor, Step, While, EndWhile, If, Then, Else, ElseIf, and EndIf keywords are used to help control Microsoft Small Basic program flow, as demonstrated in the following code:

 ' For only.
' Prints 1 through 10.
For i = 1 To 10 
  TextWindow.Write(i + " ")
EndFor

TextWindow.WriteLine("")

' For with Step.
' Prints 1, 3, 5, 7, 9 only.
For i = 1 To 10 Step 2
  TextWindow.Write(i + " ")
EndFor

TextWindow.WriteLine("")

' While.
' Prints 1 through 9 only.
i = 1 
While i < 10
  TextWindow.Write(i + " ")
  i = i + 1  
EndWhile

TextWindow.WriteLine("")

' If only.
If Clock.Year = 2010 Then
  TextWindow.WriteLine("The year is 2010.")
EndIf

' If/Else.
If Clock.Year = 2010 Then
  TextWindow.WriteLine("The year is 2010.")
Else
  TextWindow.WriteLine("The year is not 2010.")
EndIf

' If/ElseIf.
If Clock.Year = 2010 Then
  TextWindow.WriteLine("The year is 2010.")
ElseIf Clock.Year = 2009 then
  TextWindow.WriteLine("The year is 2009.")
Else
  TextWindow.WriteLine("The year is neither 2009 nor 2010.")
EndIf

You can also control program flow using the GoTo, Sub, and EndSub keywords, as demonstrated in the following code:

 Sub PrintSomeMoreText
  TextWindow.WriteLine("Fourth line to print.")  
EndSub

' Program starts here.
TextWindow.WriteLine("First line to print.")

Sub PrintSomeText
  TextWindow.WriteLine("Third line to print.")  
EndSub

TextWindow.WriteLine("Second line to print.")

PrintSomeText()

Goto printMoreText

goodbye:
TextWindow.WriteLine("Goodbye!")
Goto end

Sub ThisWillNotPrint
  TextWindow.WriteLine("This will not print unless I call it.")
EndSub

printMoreText:
PrintSomeMoreText()
Goto goodbye
  
end: