DEV Community

Tomasz Wegrzanowski
Tomasz Wegrzanowski

Posted on

100 Languages Speedrun: Episode 53: QBasic

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 - 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

IDE

And run it with F5:

Hello

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And it looks like this:

Circles

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, 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.

Code for the QBasic episode is available here.

Top comments (6)

Collapse
 
jaen profile image
Jaen • Edited

The reason why the circles do not seem round is likely because DOSBox with the default options incorrectly scales them.

On an actual VGA-compliant display, pixels in 320x200 mode are not square.

Collapse
 
taw profile image
Tomasz Wegrzanowski

Yeah, you're totally right about that.

Collapse
 
patricktingen profile image
Patrick Tingen

Talking 'bout TurboPascal... I'd love to see an episode on that one, just to see how it aged. I programmed a lot in TP5 back in the days and I still have good memories of the excellent development environment (especially for those days, early 90's)

Collapse
 
taw profile image
Tomasz Wegrzanowski

I wanted to do that too, but there doesn't seem to be any Borland Turbo Pascal version that I can just unpack and run in DOSBOX.

There's some clone (TPWDB) and some install disks, nothing that's just unpack and run.

Collapse
 
jstanley0 profile image
Jeremy Stanley

QBasic was the free version of QuickBASIC that was bundled with later versions of DOS. QuickBASIC had additional features, most notably a compiler that built standalone executables and could link external libraries.

I still have all my QB programs from the 90s…

Collapse
 
cess11 profile image
PNS11

One shouldn't miss out on the SOUND and PLAY commands if toying around with QB, together with the trig commands they allow fast and very dirty implementations of software instruments.