Data, Variables and Control Flow
Data and variables
A variable is a named store whose value can change; a constant is fixed. Common data types are integer, real (float), character, string, Boolean, and sometimes date and array. Declaring a variable reserves it and fixes its type; assigning gives it a value. Casting converts between types, and conversion can lose information, for example a real truncated to an integer.
Operators
Arithmetic operators include + - * / and often MOD (remainder) and DIV (whole-number division). Comparison operators (=, <> or !=, <, >, <=, >=) produce Boolean results. Boolean operators are AND, OR and NOT, evaluated in the order NOT, then AND, then OR, unless brackets say otherwise. Watch the classic confusion between = as assignment and == as comparison.
Control flow
Selection: if / else-if / else, and case statements; conditions can be nested. Iteration: count-controlled loops (repeat a known number of times, often FOR i = 1 TO n), condition-controlled loops (WHILE tests before the body and may run zero times; REPEAT ... UNTIL tests after and always runs at least once). Arrays and lists store many values under one name, and indexing commonly starts at 0, so the last index of an array of n items is n-1.
Traps
"5" + 3concatenates to give"53"in many languages, not 8.- A loop that counts from 0 to n inclusive runs n+1 times.
WHILEmay never execute;REPEAT ... UNTILalways executes at least once.- Accessing index n in an array of n items is out of range.
Rule of thumb
Write the loop condition and the counter update together, and check the first and last iteration on paper before trusting the loop.