The case statement may provide a readable alternative to deeply nested if conditionals. A case statement has the form case selectorExpression of caseListl: statementl;
end where selectorExpression is any expression of an ordinal type (string types are invalid) and each caseList is one of the following:
Each value represented by a caseList must be unique in the case statement; subranges and lists cannot overlap. A case statement can have a final else clause:
case selectorExpression of caseListl: statementl;
caselistn: statementn; else statements;
end where statements is a semicolon-delimited sequence of statements. When a case statement is executed, at most one of statementl... statementn is executed. Whichever caseList has a value equal to that of selectorExpression determines the statement to be used. If none of the caseLists has the same value as selectorExpression, then the statements in the else clause (if there is one) are executed.
end is equivalent to the nested conditional if I in [1..5] then Caption := 'Low'; else if I in [6..10] then Caption := 'High'; else if (I = 0) or (I in [10..99]) then Caption := 'Out of range' else
Other examples of case statements case MyColor of Red: X := 1; Green: X := 2; Blue: X = 3;
case Selection of
Compute: calculateTotal(UnitCost, Quantity); else
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.