Theory Notes/💻 Topic 1: Solution Development/11.1.1
11.1.1Grade 11

Application Development: Selection & Input Validation

Grade 11 moves from straight-line programs to programs that make decisions — and to defending those decisions against bad input.

Pascal has two selection structures. `if…then…else` handles ranges and compound conditions; `case…of` handles a list of discrete values and is far more readable when you have many of them.

Example

if mark >= 80 then WriteLn('Distinction') else if mark >= 50 then WriteLn('Pass') else WriteLn('Fail'); case grade of 'A': WriteLn('Excellent'); 'B', 'C': WriteLn('Good'); else WriteLn('Keep working'); end;

💡 Exam Tip

There is NO semicolon before else. A semicolon ends the statement, so `WriteLn('Pass');` followed by `else` is a compile error — this is the single most-tested Pascal syntax trap.

Compound conditions use the words and, or and not — and every comparison inside them must be bracketed, because in Pascal `and` binds tighter than the comparison operators.

Example

// Compile error — Pascal reads this as mark >= (0 and mark) <= 100 if mark >= 0 and mark <= 100 then // Correct if (mark >= 0) and (mark <= 100) then

Input validation
Checking data is usable before you use it — in range, right type, not empty.
StrToIntDef
StrToIntDef(s, 0) converts text to a number, returning the default instead of crashing on rubbish.
Short-circuit evaluation
Delphi mode stops evaluating an and/or as soon as the answer is certain, so you can safely test `(i <= High(arr)) and (arr[i] = target)`.