100 Languages Speedrun: Episode 53: QBasic
Tags: qbasic, basic, dos
Published: 2022-01-11 17:47:20

QBasic (or QuickBASIC) was a toy language distributed with MSDOS. It was my first language, and if it wasn't there, who knows, maybe I wouldn't be writing this series. Maybe I'd be a True Crime podcaster, a cat behaviorist, a chocolate taster, or some other profession non-developers do these days.

I don't have any code saved from these ancient times, and I don't remember much. I think I moved to Turbo Pascal quite soon. Or maybe to Legos. The past is all vague.

To run such ancient software we'll be using DOSBox, which is like a Docker for DOS.

### How to setup QBasic

* Install dosbox, like with `brew install dosbox`.
* [download QBasic from this website](https://www.qbasic.net/en/qbasic-downloads/compiler/qbasic-compiler.htm) - I took "QuickBasic 4.5 EN"
* unpack it somewhere, I unpacked it to `~/qb`
* to avoid typing the same things over and over, and maybe resize it to make it more readable on high resolution screen, create `dosbox.conf`, for me it's (also using Dvorak keyboard, which you probably don't need). Here's what mine says for this project:

```
[sdl]
windowresolution=1600x1200

[autoexec]
keyb dv103
mount c ~/qb
c:
qb
```

Then start dosbox with `dosbox dosbox.conf` - you'll be dropped directly into QBasic.

### Hello, World!

Let's create `HELLO.BAS` with this code:

```
CLS
PRINT "Hello, World!"
END
```

![IDE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4deomv77e67xggxdcjaf.png)

And run it with F5:

![Hello](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4eer6jrdeec3ejitxf5.png)

So far so good, but if we actually inspect `HELLO.BAS` file, it's not a text file, it's some weird binary. So time to take a step back, and take another look at the Save As dialog:

![Save As Dialog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qugeng7j2hr56yt4ddjc.png)

Other than Windows file endings (well, DOS line endings), it looks like a perfectly normal file now. So we can finally get to the coding.

Oh and don't resist the caps lock - QBasic will automatically convert whatever you type into uppercase except inside strings etc. You can type `print`, it will turn into `PRINT`.

### FizzBuzz

The best thing about QBasic, especially in its pre-Internet age, was builtin help system.

If you don't remember how to loop, just type `for`, press F1, and it will open the relevant help page.

With some help from the help system, we can create a FizzBuzz program:

```
CLS

FOR I = 1 TO 20
  IF I MOD 15 = 0 THEN
    PRINT "FizzBuzz"
  ELSEIF I MOD 5 = 0 THEN
    PRINT "Buzz"
  ELSEIF I MOD 3 = 0 THEN
    PRINT "Fizz"
  ELSE
    PRINT I
  END IF
NEXT I
```

### Fibonacci

QBasic wants to display just one function at a time, so to switch between functions in the same file you need to press `F2`.

```
DECLARE FUNCTION FIB! (N!)
CLS
FOR N = 1 TO 10
  PRINT "FIB(" + STR$(N) + ")=" + STR$(FIB(N))
NEXT N

FUNCTION FIB (N)
  IF N <= 2 THEN
    FIB = 1
  ELSE
    FIB = FIB(N - 1) + FIB(N - 2)
  END IF
END FUNCTION
```

The interesting thing is that I did not write that `DECLARE FUNCTION FIB! (N!)` line, QBasic added it when I saved the file. QBasic doesn't really obey the one way flow from source code text into executable program, and feels like it should all be more integrated.

### Graphics

QBasic supported some simple graphics. You could change your screen to one of supported modes. For example VGA mode 13 was 320x200 with 256 colors. Let's give it a go:

```
SCREEN 13
FOR I = 1 TO 500
  X = RND * 320
  Y = RND * 200
  C = RND * 256

  CIRCLE (X, Y), 4, C
NEXT I
```

And it looks like this:

![Circles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8wr8ormn8c1540wnlxx.png)

Do those circles seem quite as round as you'd expect, or perhaps not so much?

QBasic was OK for very simple toy "text games", but from my memory it was far too slow for any interactive graphics or graphical games. If you wanted that, you'd need something much faster like Turbo Pascal for it, possibly with a good serving of assembly for screen drawing functions. So even for the main task for all kids wanted, writing your own games, it just wasn't good enough.

### Should you use QBasic?

Obviously not. It would be extremely tedious to code in an emulator, for a system nobody uses anymore.

[Some highly nostalgic people created QB64](https://qb64.org/), which is fine as art preservation project.

However, QBasic is not actually a good code teaching tool. It was never meant for any real programming, even simple kind. Even back in the days it was just a toy for kids to get a feel for what programming is like, so maybe then they'd try the real thing. For that you're better off playing one of oh so many coding games. And then start with a real programming language like Ruby, Python, or JavaScript.

### Code

[All code examples for the series will be in this repository](https://github.com/taw/100-languages-speedrun).

[Code for the QBasic episode is available here](https://github.com/taw/100-languages-speedrun/tree/master/episode-53-qbasic).