Originale-mail to me for new edition

 

Case statements

 

The case statement provides a readable alternative to complex nested if conditionals. A case statement has the form

case selectorExpression of

  caseList1: statement1;

  ...

  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

  caseList1: statement1;

  ...

  caseListn: statementn;

else

  statements;

end

where statements is a semicolon-delimited sequence of statements. When a case statement is executed, at most one of statement1 ... 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';

  6..9: Caption := 'High';

  0, 10..99: Caption := 'Out of range';

else

  Caption := '';

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

        Caption := '';

 

Other examples of case statements:

case MyColor of

  Red: X := 1;

  Green: X := 2;

  Blue: X := 3;

  Yellow, Orange, Black: X := 0;

end;

 

case Selection of

  Done: Form1.Close;

  Compute: CalculateTotal(UnitCost, Quantity);

else

  Beep;

end;

 

Topic groups

 

See also

If statements

Structured statements: Overview

 

 

译文

 

Case语句

 

case语句为复杂的嵌套if语句提供了更加容易阅读的选择。case语句具有如下形式:

case selectorExpression of

  caseList1: statement1;

  ...

  caseListn: statementn;

end

这里的选择表达式selectorExpression是任意序数类型的表达式(串类型是无效的),并且每个情况列表caseList是下列之一:

·  数字、声明的常量,或者编译器不运行程序就能求值的表达式。并且必需与选择表达式selectorExpression类型兼容。因此,7True4 + 5 * 3'A'Integer('A')等都能作为情况列表caseList使用,而变量和大多数函数调用不能。(个别内建函数,如HiLo等可以出现在情况列表caseList中,见常量表达式。)

·  形如First..Last的子界,这里的FirstLast都必需满足上面的准则,并且First小于或等于Last

·  形如item1, ..., itemn的列表,这里的每个item也必需满足上面的准则。

 

case语句中,每个由情况列表caseList表示的值都必需是唯一的;子界和列表不能重叠。case语句的末尾还可以含有一个else子句,形如:

case selectorExpression of

  caseList1: statement1;

  ...

  caseListn: statementn;

else

  statements;

end

这里的statements是分号隔开的语句序列。当执行case语句时,statement1...statementn中的语句最多只有一个被执行。无论哪一个情况列表caseList中含有与选择表达式selectorExpression相等的值,其相应的语句statement都会被执行。如果所有的情况列表中都不含选择表达式的值,那么else子句中的语句statements将被执行(如果有该语句)。

 

case语句

case I of

  1..5: Caption := 'Low';

  6..9: Caption := 'High';

  0, 10..99: Caption := 'Out of range';

else

  Caption := '';

end;

 

等价于

 

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

        Caption := '';

 

下面是其他case语句的例子:

 

case MyColor of

  Red: X := 1;

  Green: X := 2;

  Blue: X := 3;

  Yellow, Orange, Black: X := 0;

end;

 

case Selection of

  Done: Form1.Close;

  Compute: CalculateTotal(UnitCost, Quantity);

else

  Beep;

end;

 

主题组

 

相关主题

If语句

结构语句:概述

 

 

编者注

if语句和case语句相同之处在于,最多只执行一个条件中的语句,因此有时候可以对嵌套if语句和case相互替换,但二者之间还有一些明显的差别。

 

差别一:嵌套if语句不受数据类型的约束,而case语句只能针对有序型,因此下面的嵌套if语句就无法用case语句替换:

if S = 'Book' then

  ...

else if S = 'Pen' then

  ...

else if S := 'Ruler' then

  ...

else

  ...

 

差别二:嵌套if语句可以使用多种类型的表达式,而case语句的情况列表只能使用同一种序数类型,因此下面的嵌套if语句也无法用case语句替换:

if N = 1 then

  ...

else if S = 'Hello' then

 

else if B = False then

 

else

  ...

 

差别三:嵌套if语句可以出现条件重叠,而case语句不允许,因此下面的嵌套if语句是合法的(尽管程序逻辑显然欠考虑),而case语句是不合法的:

 

if N = 1 then

  ...

else if N in [2..6] then

  ...

else if N in [3..8] then

  ...

else

  ...

 

case N of

  1: ...

  2..6: ...

  3..8: ...

else

  ...

end;