Binary conversion in Small Basic

Time to try Small Basic with a binary conversion program.  All seems straightforward, except that I found one annoying quirk with Small Basic – even though I built up the answer string by passing in the 1 and 0 in quotes, which should produce a string result, Small Basic added them as though they were numbers – so “1” + “1” + “1” would produce 3 instead of “111”.

binary code v1

Binary code v1

I ended up with two versions of the code – the first version, keeping things simple by adding strings, used “I” and “O” instead of “1” and “0”, which then produced the desired result.  Again I’ve kept it simple by not putting a loop in, so that the code needs to be run again for each number.

Small Basic itself seemed very straightforward, using the same algorithm as I used in other languages.

display from version 1

Display from version 1

The built in intellisense helps to formulate instructions correctly, it lays the code out neatly so it is easy to follow, and color coding also helps.


binary code version 2

Binary code version 2

The second version, which does output 1s and 0s rather than Is and Os, used the more cumbersome result=Text.Append(result,”1″) to build up the answer string.  This prevented the earlier problem of turning the strings into numbers, but looks more confusing and clumsy.  I wonder what other examples I’ll come across of Small Basic trying to be too helpful and correcting something that it shouldn’t do.

Result display version 2

Result display version 2

Introducing smallbasic

I learnt my programming originally using Sinclair Basic, so I was interested to see how the language has developed in the last 30 years or so.

Small Basic is produced by Microsoft as an introduction to programming.  It is designed to develop smoothly towards use of VB.NET, and programs produced in Small Basic can be easily converted into VB.NET and built on further.

small basic intellisense code completer

intellisense code completer

The feature that attracts me most towards Small Basic is the Intellisense, a built in code assistant.  As you start typing, it will help you see what the options are, and will even correct capitals and put in the indentation.

The first program, of course, is the standard hello world program.  This is achieved in one simple line in Small Basic: TextWindow.WriteLine(“Hello World”)

small basic window displaying hello world

A text window with the text displayed

This produces a window with the required line.

Using the pdf from the Small Basic website, I quickly progressed to my first proper program, a Fahrenheit to Celsius converter:

TextWindow.Write("Enter temperature in Fahrenheit: ")
TextWindow.WriteLine("Temperature in Celsius is " + celsius)

Interestingly, this produced a floating point answer rather than the integer answer you would get in something like Python, which will reply with the same data type as it took in.

Some theory here: computers can handle different types of data.  Numbers are usually stored as integers, or whole numbers, and any maths involving integers will have an integer answer.  So in Python, as in many other languages, as this code has all integers, or whole numbers, the answer would be a whole number too.  According to Python, 3/2 = 1 (it will simply discard the remainder).  To obtain the partial part of the answer, you would have to cast (change) one of the numbers to float type rather than integer, for example using print float(3)/2 or put in one of the numbers as a float, e.g. 3/2.0.  Float is another commonly used data type, referring to the decimal point as floating, i.e. its position in the number will change the value.

All that is fairly theoretical, but it does make the point that in Small Basic there is no need to worry about casting an integer to a float if you need an answer that’s not a whole number, possibly a welcome simplicity.

The next structures introduced in the Small Basic pdf are branching and looping structures – If, While, etc.  I was disappointed to note that the Goto: option still exists, as I thought that was supposed to be seen as a great weakness of BASIC programs, offering the chance to jump to a completely different place in the code.  The gosub: option has disappeared, but there is still the facility to write subroutines which are demarked by Sub and EndSub, and are called simply by calling the subroutine by name, very like defining functions in other languages, at least at first glance.

small basic graphic code

Small Basic graphic code

At this point I was feeling optimistic about Small Basic, but then the pdf seemed to divert into drawing graphics and then stop, leaving me searching around for further materials to learn from.  While the fractals and using Small Basic with a Turtle (emulating Logo) looked vaguely interesting, it didn’t really offer much opportunity to make programs that did very much except draw shapes.

small basic graphic shapes

Drawing and filling shapes

I now know how to draw and fill simple shapes (rectangle, ellipse, lines), and how to use looping to produce different shapes and patterns, but not much about how to use graphics to actually do anything interactive.

I could find no more learning materials on that site, but a small poke around found the Microsoft support site, which contains a link to a whole load of structured lessons, so that what I’ll be looking at next time I pick up Small Basic.  In the meantime, it looks promising as a step between dragging and dropping code blocks and typing out proper code: it feels like typing proper code, but the intellisense support and clear colour coding helps greatly in knowing what options are available and forming them correctly.  I would just point out very clearly to students that it is correcting missing capitals, and that when they move to a proper grown-up language capitals really will matter!

Small Basic is very limited, but the chance of growing into its big brother VB.NET means that its simplicity is not likely to prove a major disadvantage to anyone who is really making progress with coding and needs more, while it will protect and nurse the beginner through their first steps in typing code.  There is also the opportunity for learners to discuss their problems via the forum and publish their code online, so that others can download it and play with it too.