UNIHIVE

Study Hub UNIPORT CSC 180.1

CSC 180.1 · Introduction to Computer Science and BASIC Programming

Constants, variables and expressions in BASIC

Numeric and string constants, symbolic names, operators, precedence and writing formulas in BASIC.

Our notes

1 min read

Constants

  • Numeric: 25, −3.5, 1.2E3 (= 1,200), 4E−2 (= 0.04).
  • String: characters in quotes: "UNIPORT", "NAME".

Variables (symbolic names)

A variable is a named memory location whose value can change.

  • Numeric variables: a letter, or a letter followed by letters or digits: A, X1, SUM, TOTAL.
  • String variables: end with $: N$, NAME$.
  • Classic BASIC allowed only one letter or a letter and a digit; newer dialects allow longer names. Names must not be keywords and must not start with a digit.

Assignment: LET

LET X = 5 stores 5 in X. LET is optional in most dialects (X = 5). It is not an equation: X = X + 1 means "add 1 to the old value of X".

Arithmetic operators and precedence

OrderOperatorMeaning
1( )brackets first
2^power
3− (unary)negation
4* /multiply, divide (left to right)
5\ and MODinteger division, remainder (QBasic)
6+ −add, subtract (left to right)

Worked examples

  • 2 + 3 * 4 ^ 2 = 2 + 3 * 16 = 2 + 48 = 50.
  • (2 + 3) * 4 ^ 2 = 5 * 16 = 80.
  • 20 / 4 * 5 = 5 * 5 = 25 (left to right, not 20/20).
  • 17 MOD 5 = 2; 17 \ 5 = 3.

Writing formulas in BASIC

MathematicsBASIC
ax² + bx + cA*X^2 + B*X + C
(a + b)/(c − d)(A+B)/(C−D)
√(b² − 4ac)SQR(B^2 − 4*A*C)
x³/(2y)X^3/(2*Y)
πr²3.14159*R^2

The multiplication sign must always be written: 2X must be 2*X.

Relational and logical operators

=, <>, <, >, <=, >= compare values; AND, OR, NOT combine conditions: IF S >= 40 AND S < 50 THEN PRINT "D".

String operations

+ joins strings: "UNI" + "PORT" = "UNIPORT". Functions such as LEN, LEFT$, RIGHT$, MID$ work on strings.

Exams usually ask

  • State which variable names are valid and which are not (and why).
  • Evaluate BASIC expressions using precedence.
  • Write mathematical formulas as BASIC expressions.

Common mistake

Writing X/2*Y for x/(2y). It means (x/2)·y. Use brackets: X/(2*Y).

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 180.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.