Skip to content

First Programs

The quickest way to learn a language is by trying it out yourself. Just a few lines are enough to understand the fundamental principles. In this chapter, you will learn how to declare variables, perform simple calculations, and print results using PRINT. We will also look at common beginner mistakes and how to avoid them.

Declaring Variables

Variables are declared with DIM, constants with CONST. Each variable has a name, a type, and optionally an initial value.

  • DIM = allocate storage for a variable
  • CONST = constant value that cannot be changed

Example:

vb
PROGRAM  
    DIM a AS INT = 5  
    CONST PI AS REAL = 3.14159  
    PRINT a, PI ' Output: 5 3.14159  
END PROGRAM

Notes:

  • Variable names should be descriptive (counter instead of c).
  • Constants help avoid magic numbers.

Simple Calculations

The VM supports the usual operators: +, -, *, /, MOD.
Important: Integer operations are saturating.

Example: Addition & saturation

vb
PROGRAM  
    DIM x AS INT = 32000  
    DIM y AS INT = 1000  
    PRINT x + y ' Output: 32767 (saturated)  
END PROGRAM

Example: Division

vb
PROGRAM  
    DIM a AS INT = 7  
    DIM b AS INT = 2  
    PRINT a / b ' Output: 3 (INT)  
    PRINT TO_REAL(a) / b ' Output: 3.5 (REAL)  
END PROGRAM

Tip: If you expect fractional values, use REAL or convert using TO_REAL.

PRINT outputs values and text. Multiple values are separated by commas.

Example:

vb
PROGRAM  
    DIM v AS INT = 2  
    PRINT "v=", v ' Output: v= 2  
END PROGRAM

Multiple values:

vb
PROGRAM  
    DIM a AS INT = 1  
    DIM b AS INT = 2  
    PRINT "Sum:", a + b ' Output: Sum: 3  
END PROGRAM

Debugging tip: Use clear prefixes like [DBG] so outputs are easy to spot.

vb
PRINT "[DBG] counter=", counter

Common Beginner Mistakes

IssueCauseFix
Program stops immediatelyMissing END PROGRAMAlways end scripts with END PROGRAM
Division by zeroDenominator = 0Check first: IF b <> 0 THEN …
Unexpected roundingInteger division instead of REALUse TO_REAL
Block used without instanceBlock called without DIMInstantiate first: DIM T AS TON
Comment formatting errorsIncorrect ' or REM usageAlways start comments with ' or REM

Summary

  • Declare variables with DIM, constants with CONST.
  • Arithmetic works as expected, but saturates.
  • PRINT is the most important tool for understanding and debugging.
  • Common mistakes are easy to avoid once you know the rules.