In QBasic, it is customary to use the DIM
statement to create variables, giving them a name and a type. However, this isn't mandatory, QBasic can also derive a type by the suffix of the variable's name. Since you can't declare and initialise a variable at the same time, it's often wise to skip the DIM
in codegolf. Two snippets that are functionally identical*:
DIM a AS STRING: a = "example"
a$ = "example"
* Note that this does create two different variable names.
We can specify the type of the variable by adding $
to the end of a variable name for strings, !
for single precision numbers and %
for doubles. Singles are assumed when no type is specified.
a$ = "Definitely a string"
b! = "Error!"
Note that this also holds for arrays. Usually, an array is defined as:
DIM a(20) AS STRING
But arrays also don't need to be DIM
med:
a$(2) = "QBasic 4 FUN!"
a$
is now an array for strings with 11 slots: from index 0 to and including index 10. This is done because QBasic has an option that allows both 0-based and 1-based indexing for arrays. A default array kind of supports both this way.
Remember the twenty-slot array we DIM
med above? That actually has 21 slots, because the same principle applies to both dimmed and non-dimmed arrays.
I'm curious about your motivation. I haven't used QBASIC since my 10th grade programming class. Amazing how I saved directly to 1.44 floppy disks without any form of version control and (usually) avoided catastrophic failures. – Andrew Brēza – 2017-11-11T03:21:31.060
5@AndrewBrēza Motivation? The same as my motivation to golf in any language: for fun! I enjoy writing small programs in QBasic (though I wouldn't want to use it for anything serious). There's also the added bonus that it's got sound and graphics (both text and pixel) built in, which my preferred "real" language, Python, does not. – DLosc – 2017-11-11T06:21:28.587
It's much easier to write graphical games in QBasic than in python. – Anush – 2018-06-29T18:46:13.320
If anyone wants to try graphical QBasic applications directly in the browser they could use this: https://github.com/nfriend/origins-host
– mbomb007 – 2018-12-19T15:56:56.280