Variables, Constants and Literals
A variable is an object in a program whose value can be modified during execution, declared with the keyword VAR followed by its name and type, e.g. VAR A: Integer. Basic data types include Integer, Real, Boolean, Character and String. A constant is an object whose value cannot be modified, declared with CONST, e.g. CONST PI = 3.14. Variables and constants are characterized by an identifier (name), a value (content) and a type (domain). A literal is anything fixed by the programmer during coding, usually written in double quotes, e.g. “Enter a number”.
Basic Instructions
- Input: reads a value from the keyboard using
read(),readln()orGet, e.g.read(a);. - Output: displays information using
write(),writeln()orprint, e.g.write("bonjour");. - Assignment: assigns a value to a variable using ← or :=, e.g.
sum ← a + b;. - Increment/decrement: incrementing adds 1 (or a fixed value) to a counter (
i ← i+1); decrementing subtracts 1 (j ← j-1).
Basic Algorithmic Control Structures
i. Sequence Structure
Executes a set of instructions one after the other, in the order given – from the first to the last.
ii. Selection Control Structure (alternative structure)
Chooses the instruction(s) to execute based on the validity of a condition, e.g. if...then...else and case...of. Selection structures can be nested, so that if condition 1 is true, execution moves on to check condition 2, and so on. case...of is a multiple selection structure used when many choices depend on the value of a single variable, e.g. printing the day of the week based on a day number.
iii. Repetition (Iteration) Structure
Executes an instruction or set of instructions repeatedly until a certain condition is reached, or while a condition is true – also known as a loop. Examples:
- while…do: the condition is evaluated first; instructions run only while it remains true.
- repeat…until: the instructions run first, then the condition is evaluated; they repeat until the condition becomes true (so the loop always runs at least once).
- for…to…do: a variable is given a low or high limit value, automatically incremented or decremented after each execution, until the limit is reached.
Remark: some problems are recursive in nature, solved by repeated application of a solution to its own values until a condition is reached (e.g. the Factorial, sum and Fibonacci functions). A recursive algorithm is one that calls (invokes) itself during execution.