Case Statements

The case statement may provide a readable alternative to deeply nested if conditionals. A case statement has the form case selectorExpression of caseListl: statementl;

caseListn: statementn;

end where selectorExpression is any expression of an ordinal type (string types are invalid) and each caseList is one of the following:

  • A numeral, declared constant, or other expression that the compiler can evaluate without executing your program. It must be of an ordinal type compatible with selectorExpression. Thus 7, True, 4 + 5 * 3, 'A', and integer('A') can all be used as caseLists, but variables and most function calls cannot. (A few built-in functions like Hi and Lo can occur in a caseList. See Constant expressions.)
  • A subrange having the form First..Last, where First and Last both satisfy the criterion above and First is less than or equal to Last.
  • A list having the form item1,..., itemn, where each item satisfies one of the criteria above.

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.

The case statement case i of

  1. 5: Caption := 'Low';
  2. 9: Caption := 'High';


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

Done: Forml.Close;

Compute: calculateTotal(UnitCost, Quantity); else



Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

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.

Get My Free Ebook

Post a comment