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
- Chapter 4: Conditions and Branching (Wiki)
- Chapter 5: Loops (Wiki)
- Small Basic: Control Statements (Wiki)
- Most Popular Algorithm?
Comments
- Anonymous
December 30, 2016
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.' LoopGraphicsWindow.PenColor = "Orange"x = Turtle.Xy = Turtle.YWhile (Turtle.X x) Or (Turtle.Y (y - 100)) Turtle.Move(4.5) Turtle.Turn(2)EndWhile__________ - Anonymous
December 30, 2016
% 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) = .....- Anonymous
January 02, 2017
Hello, moonlight. Good question! 2° means that this is a 180 sided regular polygon. So, the circumference (2πr) equals 4.5 * 180 [pixels]. - Anonymous
January 02, 2017
To be exact, r * sin(1°) = 4.5 / 2 [pixels].
- Anonymous
- Anonymous
January 04, 2017
Great code patterns! Love having the See Also links!