Small Basic: Control Statements

Small Basic: Control Statements

In the Microsoft Small Basic programming language, there are five types of control statements - If statement, Goto statement, While loop statement, For loop statement, and subroutine call statement.  These statements build up with only fourteen keywords of Small Basic.  This article is an overview of these control statements.  Control statements change program flow respectively.

If statement

If statement or If clause is for making conditional branch.  If you'd like to show "AM" or "PM" by checking clock, following code and chart are for that.  There is another keyword "ElseIf" for this clause.  For detail about "ElseIf", see the next section.

' Start
hour = Clock.Hour             ' Get hour
If hour < 12 Then             ' hour < 12?
  TextWindow.WriteLine("AM")  ' Yes: Write "AM"
Else
  TextWindow.WriteLine("PM")  ' No: Write "PM"
EndIf
' End

Goto statement

Goto statement can jump anywhere labeled place in the program.  Label is written with colon such as "start:".  Following sample program checks input text, and if it is "Y" or "N" then shows "True" or "False", otherwise input text again with Goto statement in line 10.

1.' Start
2.start:
3.TextWindow.Write("1+1=2 Y/N? ") ' Write "1+1=2 Y/N? "
4.ans TextWindow.Read()         ' Read ans
5.If ans = "Y" Then               ' ans?
6.  TextWindow.WriteLine("True")  ' "Y": Write "True"
7.ElseIf ans = "N" Then
8.  TextWindow.WriteLine("False") ' "N": Write "False"
9.Else
10.  Goto start                    ' other: input again
11.EndIf
12.' End

While loop statement

While loop statement repeats statements while condition is true.  In following case, while condition "i <= 10" is true, "n = n + 1" and "i = i + 2" are repeated. 

' Start
n = 0
i = 2
While i <= 10
  n = n + i
  i = i + 2
EndWhile
TextWindow.WriteLine(n)    ' Write n
' End

For loop statement

For loop statement repeats statements with increasing or decreasing a variable.  In following case, statement "n = n + i" is repeated with increasing a variable "i" from 2 to 10 by inclement 2.  And this sample shows same result with the sample above.

' Start
n = 0
For i = 2 To 10 Step 2
  n = n + i
EndFor
TextWindow.WriteLine(n) ' Write n
' End

Subroutine call statement

Subroutine call statement calls subroutine as follows. 

' Start
A()                              ' Call A
' End
Sub A
  TextWindow.WriteLine("A")      ' Write "A"
EndSub                           ' Return

See Also

Sort by: Published Date | Most Recent | Most Useful
Comments
  • Excellent article, why didn't you use your own code formatter?

  • Thanks, litdev.  This article was written before I developed the code formatter.  Anyway, code blocks have been little broken after I wrote them.  So, I will update them with my code formatter.

Page 1 of 1 (2 items)