Programming basics
From design to code
- A design (flowchart or structured English) becomes pseudocode, then a real language.
- Find the variables and types, turn boxes into statements, then trace a small input to check.
- First, the basic building blocks: variables, expressions and built-in routines.
Constants and variables
- A constant holds a value that never changes; a variable holds one that may.
CONSTANT Pi ← 3.14159
DECLARE Radius : REAL
Radius ← 5
Area ← Pi * Radius * Radius
- Use constants for fixed recurring values (
Pi,MaxScore) — clearer, and changed in one place.
Practice
A constant is best used for:
Constants hold values that never change, making code clearer and easy to update in one place.
Assignment and expressions
- Assignment uses
←:Total ← Total + 1. - Operators: arithmetic
+ - * /, plusDIV(integer division) andMOD(remainder):7 DIV 2 = 3,7 MOD 2 = 1. - Comparisons
= <> < > <= >=; logicAND OR NOT. - Precedence (high → low):
NOT→* / DIV MOD→+ -→ comparisons →AND→OR. Use brackets when unsure.
Practice
What is the value of 7 DIV 2?
DIV is integer division: 7 ÷ 2 = 3 remainder 1, so DIV gives 3.
Practice
What is the value of 7 MOD 2?
MOD gives the remainder: 7 ÷ 2 leaves remainder 1.
Practice
Which symbol assigns a value to a variable in this pseudocode?
Assignment uses ←; = is comparison.
Built-in library routines
- Don't reinvent common tasks — use library routines:
- String:
LENGTH(s),LEFT(s,n),RIGHT(s,n),MID(s,start,len),UCASE(s),LCASE(s). - Numeric:
INT(x),ROUND(x),ABS(x),RANDOM(). - Conversion:
STR(x)(number → string),VAL(s)(string → number).
Practice
What does the library routine LENGTH("Hello") return?
LENGTH returns the number of characters — "Hello" has 5.
You've got it
Key idea
- constant = fixed value; variable = changeable; declare both with a type
- assignment is
←;DIV= integer division,MOD= remainder - precedence:
NOT→* / DIV MOD→+ -→ comparisons →AND→OR - reuse library routines (
LENGTH,ROUND,STR/VAL) instead of rewriting them