Small Basic for Little Kids Series #02 – Loop

The single most important concept in programming is loop, I think.  And yet, it's (usually) not an easy concept for kids to grasp. I remember when I was young, I had to struggle with the idea of loop and N=N+1.

Back then, a loop in BASIC might look like this:

10 PRINT "123"
20 GOTO 10

GOTO statement had and continues to have the reputation of producing spaghetti code.  However, based on my estimation, it does make it much easier to explain the loop πŸ™‚

Of course, the two-line code above will go into infinite loop and the only way to stop it is by forcing stop execution of the program, e.g., by hitting Ctrl-C.

So, how to make 10 loops?  Here is one way to do it.

10 N=1
40 IF N>=10 THEN END
30 N=N+1
50 GOTO 20

Another way is to use the FOR-NEXT construct.  I think it goes something like this:

10 FOR I = 1 TO 10

But that's the old syntax.  I am certainly dating myself here.
A couple of things you will notice though.
One is the presence of line numbers.
Another is the capitalization of all code.

With MS Small Basic, the code will look like this:

For i = 1 To 10

Here is the output:

Press any key to continue...

In most programming language, white spaces (hence indentation) is insignificant  (with Python being a notable exception)
Therefore, for good programming practice, it's important to indent text appropriately.

For example, with the TextWindow.WriteLine(i) being indented here, it is much easier to see that it's a FOR block.

That being said, it's still not a walk in the park to explain this to my son.

You would be surprised at how kids learn things.  They learn things they want to learn, the way they want, when they want!

I spent about 20 minutes each on two different occassions explaining loop to him.  I thought he got it.  He appeared to understand it.  But when asked a day later, he is like "I don't know."

It stayed this way until over the holiday weeks when he finally got it.  It was so intersting how it happened that I simply have to share it.

He was playing Minecraft (one of his favorite games for now), and was very excited to explain to me how he figured out a way to "trick" the game.

Since I don't play Minecraft, I am not sure if I can explain this well, but will try.  For some reason, he needed to change the time in the game (turning from night to day, or vice versa) by activating a switch or something like that.

The idea he came up with was to construct a circular track and then made the truck to go around and around. Every time it gets to a specific spot on the track, something will be triggered which will then change the time and what not.

Upon hearing this, I seized the opportunity to explain to him that is in fact a LOOP, an infinite loop that is.

The circular track is the loop itself.  The action that's triggered each time the truck go through a specific spot is the content within the loop.

He thought about it and exclaimed he got it.  I do think he really got it this time πŸ™‚

So the moral of the story is that one doesn't have to sit in front of Visual Studio (or Kid Studio) all day long to learn programming.  Learning happens all the time and in mysterious ways, and especially so for little kids.

Lastly, here is something for a little fun:

For i = 1 To 100000

Can you explain the animation effect of numbers moving?

If you don't get it right away, try this for comparison:

For i = 1 To 100000


Can you see why printing "123" and "1234" produce different effects on your eyes?

Here is a hint.

Usually, the width of the text console is an even number.  The length of "123" string is an odd number.


Comments (3)

  1. anonymouscommenter says:

    I don't know BASIC, but it appears that the given example for making a loop that executes 10 times in BASIC has a typo or something- would it not reset n to n=1 at the start of each loop iteration?

  2. Ray FAST says:

    You are absolutely, Tangmeister.  Line 50 should go back to line 20, not to 10.  I just edited the post to reflect that.

    This is the exact kind of sloppiness that introduces bugs πŸ™‚  I wrote it from memory and figured it's simple enough and should just work.  A prudent thing to do would have been to test it out on a BASIC emulator (with line numbers), no matter how simple the code is.   Will tell the story to my son.  He should get a kick out of it — Papa also makes mistakes πŸ™‚

  3. Ah Loops. My favorite thing in the world. =^)

Skip to main content