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

Comments (5)

  1. YLed says:

    Fine article !
    And if a replace the '' loop'' part of your program LMR321 with this below, the turtle will draw a circle without math formula.
    in fact, it's a succession of straight lines that the turtle draws, but the fact that it rotates 2 pixels every time, the eye does not perceive these lines, we perceive them as a whole, a circle.
    ________________________________________________________________________________________________________
    ' Loop
    GraphicsWindow.PenColor = "Orange"
    x = Turtle.X
    y = Turtle.Y
    While (Turtle.X x) Or (Turtle.Y (y - 100))
    Turtle.Move(4.5)
    Turtle.Turn(2)
    EndWhile
    __________________________________________________________________________________________________________________

  2. moonlight says:

    % them as a whole, a circle.
    Now, what is your radius r as function of 4.5 and 2 (with 4 decimals)? r(4.5, 2) = .....

    1. Hello, moonlight. Good question! 2° means that this is a 180 sided regular polygon. So, the circumference (2πr) equals 4.5 * 180 [pixels].

    2. To be exact, r * sin(1°) = 4.5 / 2 [pixels].

  3. Great code patterns! Love having the See Also links!

Skip to main content