Some simple code examples for QB64

Posted on: Posted on

QB64 is a modern dialect of QBasic/QuickBASIC, designed to be compatible with classic BASIC while adding new features and supporting modern operating systems. It’s great for learning programming because of its simplicity and immediate visual/auditory feedback.

Here are some simple code examples to get you started:


1. Hello World (The Absolute Basic)

This program clears the screen, prints a message, and then waits for a keypress before exiting.

' A simple "Hello, World!" program

CLS ' Clear the screen
PRINT "Hello, QB64!" ' Display the message

SLEEP ' Pause until a key is pressed. Prevents the window from closing immediately.

2. Variables and User Input

This example demonstrates how to declare variables, take input from the user, and display it.

' Program to get user's name and age

CLS ' Clear the screen

DIM userName AS STRING ' Declare a variable for the user's name (text)
DIM userAge AS INTEGER ' Declare a variable for the user's age (whole number)

' Get input from the user
INPUT "What is your name? ", userName
INPUT "How old are you? ", userAge

' Display a personalized message
PRINT "Hello, " + userName + "!"
PRINT "You are " + STR$(userAge) + " years old." ' STR$ converts number to string for concatenation

' Calculate and display age in 10 years
PRINT "In 10 years, you will be " + STR$(userAge + 10) + " years old."

SLEEP

Note: While QB64 often automatically converts numbers to strings for PRINT statements, using STR$() is good practice for explicit string concatenation with the + operator.

3. Basic Arithmetic Operations

Shows how to perform addition, subtraction, multiplication, and division.

' Demonstrates basic arithmetic operations

CLS

DIM num1 AS INTEGER
DIM num2 AS INTEGER
DIM result AS SINGLE ' Use SINGLE for potential decimal results in division

num1 = 20
num2 = 7

PRINT "Numbers are: "; num1; " and "; num2
PRINT

PRINT "Addition:       "; num1; " + "; num2; " = "; num1 + num2
PRINT "Subtraction:    "; num1; " - "; num2; " = "; num1 - num2
PRINT "Multiplication: "; num1; " * "; num2; " = "; num1 * num2
PRINT "Division:       "; num1; " / "; num2; " = "; num1 / num2
PRINT "Integer Division: "; num1; " \ "; num2; " = "; num1 \ num2 ' Integer division
PRINT "Modulo (Remainder): "; num1; " MOD "; num2; " = "; num1 MOD num2 ' Remainder

SLEEP

4. Conditional Statements (IF…THEN…ELSE)

Makes decisions based on conditions.

' Checks if a number is positive, negative, or zero

CLS

DIM num AS INTEGER
INPUT "Enter a number: ", num

IF num > 0 THEN
    PRINT "The number is positive."
ELSEIF num < 0 THEN ' ELSEIF for multiple conditions
    PRINT "The number is negative."
ELSE ' If neither of the above is true
    PRINT "The number is zero."
END IF ' Don't forget to close the IF block

SLEEP

5. Loops (FOR…NEXT and DO…LOOP)

Demonstrates how to repeat actions.

a) FOR…NEXT Loop

' Counts from 1 to 5 and then from 10 down to 0

CLS

PRINT "Counting up to 5:"
FOR i = 1 TO 5 ' Loop from 1 to 5, incrementing by 1 (default STEP)
    PRINT i
NEXT i ' End of the loop, 'i' is optional but good practice

PRINT
PRINT "Counting down from 10 by 2s:"
FOR j = 10 TO 0 STEP -2 ' Loop from 10 down to 0, decrementing by 2
    PRINT j
NEXT j

SLEEP

b) DO…LOOP WHILE / UNTIL Loop

' Demonstrates a DO...LOOP that continues until a specific condition is met

CLS

DIM password AS STRING
password = "" ' Initialize with an empty string

DO ' Start of an indefinite loop
    INPUT "Enter the secret password: ", password
    IF password <> "QB64rules" THEN ' If password is not correct
        PRINT "Incorrect password. Try again."
    END IF
LOOP WHILE password <> "QB64rules" ' Continue looping while password is NOT "QB64rules"

PRINT "Password accepted! Welcome."

SLEEP

6. Subroutines and Functions (Modularity)

Organizing code into reusable blocks.

' Demonstrates using SUBs (subroutines) and FUNCTIONs

' DECLARE statements are good practice for QB64, especially for larger programs.
' They tell the compiler about SUBs/FUNCTIONs defined later.
DECLARE SUB DisplayMessage (message AS STRING)
DECLARE FUNCTION AddNumbers (numA AS INTEGER, numB AS INTEGER) AS INTEGER

CLS

' Calling a subroutine
DisplayMessage "Hello from the main program!"

' Calling a function and storing its return value
DIM sumResult AS INTEGER
sumResult = AddNumbers(10, 5)
PRINT "The sum of 10 and 5 is: "; sumResult

' You can also call a SUB without CALL keyword in QB64:
' DisplayMessage "Another message!"

SLEEP

' --- Subroutine Definition ---
SUB DisplayMessage (message AS STRING)
    PRINT "---"
    PRINT "Message: " + message
    PRINT "---"
END SUB

' --- Function Definition ---
FUNCTION AddNumbers (numA AS INTEGER, numB AS INTEGER)
    ' Functions return a value by assigning to the function's name
    AddNumbers = numA + numB
END FUNCTION

7. Simple Graphics

Drawing shapes on the screen.

' Simple graphics demonstration

CLS
SCREEN 12 ' Set to graphics mode: 640x480 pixels, 16 colors

' Draw a single pixel (yellow)
PSET (320, 240), 14 ' (x, y) coordinates, color (14 is yellow)

' Draw a line (green)
LINE (100, 100)-(500, 400), 10 ' (x1, y1)-(x2, y2), color (10 is light green)

' Draw a box (blue)
LINE (50, 50)-(200, 200), 9, B ' (x1, y1)-(x2, y2), color (9 is light blue), B for Box

' Draw a filled box (red)
LINE (250, 50)-(400, 200), 12, BF ' BF for Box Fill (12 is light red)

' Draw a circle (magenta)
CIRCLE (500, 150), 75, 13 ' (center_x, center_y), radius, color (13 is light magenta)

' Print text in graphics mode
COLOR 15 ' Set text color to white
LOCATE 28, 1 ' Move cursor to row 28, column 1
PRINT "Press any key to close the graphics window..."

DO
    ' Wait for a keypress to keep the window open
LOOP UNTIL _KEYHIT

' _SCREENCLOSE ' Optional: Close the graphics screen explicitly
END ' Terminate the program

Note: _KEYHIT is a QB64 specific function. In old QBasic, you might use WHILE INKEY$ = "" : WEND.

8. Sound and Music

Making beeps and playing simple tunes.

' Simple sound and music demonstration

CLS

PRINT "Playing a short beep..."
BEEP ' A simple system beep

SLEEP 1 ' Pause for 1 second

PRINT "Playing a custom sound (A note for 0.5 seconds)..."
SOUND 440, 9 ' Frequency (Hz), Duration (in 18.2ths of a second, so 9 is about 0.5s)

SLEEP 1

PRINT "Playing a simple tune (CDEFGAB scale)..."
' PLAY command uses a special music string language
PLAY "O3 T120 CDEFGAB" ' O3=Octave 3, T120=Tempo 120 beats/min, then notes C to B

SLEEP 1

PRINT "More complex tune with different lengths and octaves..."
PLAY "L8 CDEF L4 GAB L16 C" ' L8=Eighth notes, L4=Quarter notes, L16=Sixteenth notes
PLAY "O2 C.D.E.F. O3 GAB O4 C" ' . means dotted note (1.5x length)

SLEEP

How to use these examples with QB64:

  1. Open QB64: Launch the QB64 IDE (Integrated Development Environment).
  2. New File: It usually starts with a blank editor. If not, go to File > New.
  3. Copy Code: Copy and paste any of the examples into the editor.
  4. Run: Press F5 to compile and run the program.
  5. Save (Optional but Recommended): Go to File > Save As..., give your file a name (e.g., helloworld.bas), and save it. QB64 files typically use the .bas extension.

Have fun exploring QB64! It’s a fantastic environment for learning programming fundamentals.

Leave a Reply

Your email address will not be published. Required fields are marked *