PowerBASIC Statements

DECLARE (External Function)

Syntax: DECLARE FUNCTION|SUB name LIB "dll" ALIAS "export" (params) AS type

Declares an external function imported from a Windows DLL. Supports BYVAL and BYREF parameters. Multiline parameter lists with _ continuation are supported.

DECLARE FUNCTION MessageBoxA LIB "user32.dll" _
    ALIAS "MessageBoxA" ( _
    BYVAL hWnd AS QUAD, _
    BYVAL lpText AS ASCIIZ PTR, _
    BYVAL lpCaption AS ASCIIZ PTR, _
    BYVAL uType AS LONG _
) AS LONG

Assignment

Syntax: variable = expression | FUNCTION = expression

Assigns a value to a variable. FUNCTION = expr sets the return value of the current function.

x = 42
name$ = "Hello"
FUNCTION = 0

PRINT

Syntax: PRINT expr [; expr ...]

Outputs text to the console. Multiple expressions separated by ; are concatenated. A trailing expression adds a newline.

PRINT "The value is "; x
PRINT "Hello, "; name$

IF / ELSE / END IF

Syntax: IF condition THEN statements [ELSE statements] END IF

Conditional execution. Supports ELSEIF for multi-way branching.

IF x > 10 THEN
    PRINT "Large"
ELSEIF x > 0 THEN
    PRINT "Small"
ELSE
    PRINT "Zero or negative"
END IF

FOR / NEXT

Syntax: FOR var = start TO end [STEP n] : statements : NEXT [var]

Iterates a variable from start to end with optional STEP increment.

FOR i = 1 TO 10
    PRINT i
NEXT i

FOR j = 10 TO 1 STEP -2
    PRINT j
NEXT

WHILE / WEND

Syntax: WHILE condition : statements : WEND

Repeats a block of statements while the condition is true.

WHILE x > 0
    PRINT x
    x = x - 1
WEND

GOTO

Syntax: GOTO label

Unconditional jump to a label. Labels are plain identifiers followed by :.

IF error THEN GOTO Cleanup
PRINT "All OK"
Cleanup:
PRINT "Done"

GOSUB / RETURN

Syntax: GOSUB label ... label: ... RETURN

Calls a local subroutine and returns when RETURN is encountered.

GOSUB PrintHeader
...
PrintHeader:
    PRINT "=== Header ==="
    RETURN

CALL

Syntax: CALL procedure(args)

Calls an external or user-defined procedure. For external WinAPI calls, arguments are staged according to the Windows x64 calling convention (first 4 in registers, rest on stack).

CALL MessageBoxA(0, STRPTR(msg$), "Title", 0)

File I/O Statements

OPEN

Syntax: OPEN "file" FOR OUTPUT|APPEND|INPUT AS #n

Opens a file for output (write), append, or input (read). The file number #n is used in subsequent I/O statements.

OPEN "data.txt" FOR OUTPUT AS #1
OPEN "data.txt" FOR APPEND AS #2
OPEN "data.txt" FOR INPUT AS #3

PRINT #

Syntax: PRINT #n, expr [; expr ...]

Writes text to an open file.

PRINT #1, "Line of text"
PRINT #1, "Value: "; x

INPUT #

Syntax: INPUT #n, variable

Reads data from an open file into a variable.

INPUT #3, line$

CLOSE

Syntax: CLOSE #n

Closes an open file.

CLOSE #1