This article is currently available in English. Your interface language stays selected.

02 · REMEMBER A VALUE

Variables and expressions

Give values names in BASIC, work with numeric expressions, and use string variables with a dollar-sign suffix.

Try these BASIC examples in the Web IDE Terminal.

Give a value a name

A variable lets a program remember a value. Assignment evaluates the expression on the right of = and stores it under the name on the left.

BASIC
x = 25
PRINT x * 4

The numeric result is 100. Classic BASIC numeric output may include spacing; the value is the point of this example.

Store some text

In classic BASIC syntax, a dollar sign at the end of a variable name marks a string variable. Use + to join strings.

BASIC
name$ = "Cortex"
PRINT "Hello, " + name$ + "."

The expected text is Hello, Cortex.. A string is text, even when it contains digits.

Make the calculation clear

Expressions combine values, variables, and operators. Parentheses make the intended grouping explicit.

BASIC
rectWidth = 12
rectHeight = 8
area = rectWidth * rectHeight
perimeter = 2 * (rectWidth + rectHeight)
PRINT area
PRINT perimeter

The numeric results are 96 and 40. Cortex uses QBASIC-style typing and coercion rules. Use HELP in Terminal for language reference entries.

Try another value

Change width or height and predict both results before running the program. Then learn how to repeat work with a loop.

← Back to Learn