Algorithm Basics in Small Basic

Algorithm is combinations of code patterns.  Today, I'd like to introduce three basic code patterns.  I also wrote a sample program for this blog: LMR321.

Sequence

Sequence of statements is the simplest pattern of code.  But sometimes the order of statements becomes very important.

Following two code blocks show the different results.

 Turtle.Move(100)  ' move first
Turtle.Turn(90)
 Turtle.Turn(90)  ' turn first
Turtle.Move(100)

Loop

In a program, we sometimes need to repeat something.  We can also write same lines of code, but a loop makes it simpler.  Following two code blocks show the same results.

 TextWindow.WriteLine("Hello World!")
TextWindow.WriteLine("Hello World!")
TextWindow.WriteLine("Hello World!")
TextWindow.WriteLine("Hello World!")
 For i = 1 To 4
  TextWindow.WriteLine("Hello World!")
EndFor

Selection

There are some cases we'd like to change doing with conditions such as input data or current status.  We can do this kind of selection with If statement like following code.

 If Turtle.Y < yCenter Then
  TextWindow.WriteLine("UPPER")
Else
  TextWindow.WriteLine("LOWER")
EndIf

See Also