Friday Fun: Commodore Coding Concepts

In code there are a few fundamental concepts. Sequence, Selection, Iteration, Assignment. We shall take a look at them in BASIC AND Python

In BASIC?

Yeah Beginners All Purpose Symbolic Instruction Code the Python of the 1980s! I remember collecting a series of magazines in the 1980s and each magazine was a game / program in BASIC.

BASIC is a human readable (high level) language that can be easily read and understood by a person. In this blog post I am going to write BASIC code using a Commodore 64 emulator, C64 Forever from Cloanto which is a paid for product. But there is a free alternative called VICE which I use on my Linux machines, including my arcade cabinet for emulation purposes.

In Python?

Python is the current popular language for learners, and with good reason. It is easy to read, easy to learn and there are many tutorials and guides to follow on the Internet. To compare BASIC to a "modern" language, I'm going to write the same code in Python, so we can see the similarities in concepts.

Hi reader!

I never put my blog posts behind paywall or pop ups because quite frankly that is annoying and prevents everyone from accessing the content. I will always keep my blog content free of charge. But I do ask that if you are able and willing, that you buy me a "coffee" as it helps me to pay for hosting this blog, and to buy stuff to hack from Poundshops / Dollar Stores / Aliexpress which are used in free projects and reviews on this blog. It is You dear reader who make this possible, and I am immensely grateful for your support.

Thanks!

Sequence

Have you ever baked a cake? Well that recipe is a sequence of instructions that we read line by line and then perform the task before moving on to the next line.

A sequence is also known as an algorithm, which to a kid is a big, scary word. It is also hard to spell!

Lets create a simple sequence of code.

10 PRINT "HELLO WORLD"
20 PRINT "STAY SAFE, AND WASH YOUR HANDS"

So what do those two lines do?

Each of these lines prints a string - characters of text, numbers of punctuation to the screen. These two lines form a very short sequence of code.

In Python

print("HELLO WORLD")
print("STAY SAFE, AND WASH YOUR HANDS")

Selection

"What would you like for dinner tonight?" This is a decision, and we make decisions without even thinking about it. We select what we want based on conditions. If we have chips, then we have chips, else if we have beans, then we have beans, else we go hungry!

10 INPUT A
20 IF A = 1 THEN GOTO 50
30 PRINT "THIS IS THE ELSE SECTION"
40 GOTO 60
50 PRINT "YOU PRESSED 1, SO THE IF WAS TRUE"
60 PRINT "CONDITIONAL TEST COMPLETE"
70 END

In this BASIC code we ask on line 10 for the user to type something in, and this is then saved to a variable called A.

Line 20 sees the input stored in A tested against a hard coded value of 1. So in this case if the user input matches 1, then the code will go to line 50. At line 50 a message is printed to the screen which confirms that the user chose correctly. We then move to line 60 and another line of text is printed. With line 70 we end the code. But what if the user enters anything apart from 1? Well then at line 20 we do not skip to line 50, and instead move on to line 30, where a message is printed to tell us that we have activated the else condition. Line 40 skips our code to line 60 which prints that the test is complete, before ending the code on line 70.

In Python



The Python code is a little different in that the if condition code is at the top of the sequence, and the else is at the bottom. In BASIC this was the other way around.

a = input()
if a == "1":
    print("YOU PRESSED 1, SO THE IF WAS TRUE")
else:
    print("THIS IS THE ELSE SECTION")
print("CONDITIONAL TEST COMPLETE")

Iteration

This is typically a loop which will repeat a set number of times, or until a condition is met.

So here are five lines of BASIC which will print HELLO WORLD on the screen five times.

10 PRINT "HELLO WORLD"
20 PRINT "HELLO WORLD"
30 PRINT "HELLO WORLD"
40 PRINT "HELLO WORLD"
50 PRINT "HELLO WORLD"

Here is the same output, but using only three lines of code and a for loop which will iterate (loop) five times before ending.

10 FOR I = 1 TO 5
20 PRINT "HELLO WORLD"
30 NEXT I

In Python

for i in range(5):
    print("HELLO WORLD")

A loop that never ends!

There are times when we need a loop to constantly iterate and never stop. In Python this is via a while True: loop, in Scratch it is the Forever block.

With BASIC we have a simple sequence which uses GOTO to loop our code.

10 PRINT "HELLO WORLD"
20 PRINT "STAY SAFE, AND WASH YOUR HANDS"
30 GOTO 10

In the above code, borrowed from the Sequence section of this post, we add a third line, line 30 which directs the code back to line 10.

In Python

while True:
    print("HELLO WORLD")
    print("STAY SAFE, AND WASH YOUR HANDS")

Assignment

Assignment is when data is stored in a data storage object. The most basic example is a variable which we used in Selection. A variable is assigned a value using a single equals sign =.

Lets create a variable called A and store the string "Hello World" inside of it.

Oh no!! We have a ?TYPE MISMATCH ERROR but what does that mean? Well unlike Python, BASIC requires us to ensure the correct data type is stored in the correct variable.

If we intend to store an integer (a number with no decimal place) or a float (a number with a decimal place) then we can use A as a variable name. In the above example I created two variables, A and B and in A I stored a float value of 2.5, and in B I stored an integer value of 10. As you can see, I also used the variables to write a short equation to multiply A by B.

A = 2.5
B = 10
PRINT A * B

But I want to store a string in a variable!

Fear not we can do this by using a $ in the variable name.

A$ = "FOR HACKS VISIT BIGL.ES"
PRINT A$

In Python



In the Python code, I created three variables, a which stores the float value 2.5, b which stores the integer value 10, and finally c which stores a string value "Python 3 FTW!". We print the calculation of a * b and then multiply the string c by the value of b, this prints the string 10 times to the shell.

a = 2.5
b = 10
c = "Python 3 FTW!"

print(a*b)

print(c*b)

Final Thoughts

BASIC is a versatile language and it isn't too far removed from Python for eager coders to investigate. It is fun to look back at older technologies, understand how they work and seek to use them again in projects.

For the next Friday Fun blog post I shall be using a real Commodore 64 and BASIC code to control a motor using the MAKER-DRIVE board that I reviewed in a past Tuesday Tooling blog post.

Happy Hacking