Originale-mail to me for new edition

 

If statements

 

There are two forms of if statement: if...then and the if...then...else. The syntax of an if...then statement is

if expression then statement

where expression returns a Boolean value. If expression is True, then statement is executed; otherwise it is not. For example,

if J <> 0 then Result := I/J;

The syntax of an if...then...else statement is

if expression then statement1 else statement2

where expression returns a Boolean value. If expression is True, then statement1 is executed; otherwise statement2 is executed. For example,

if J = 0 then

  Exit

else

  Result := I/J;

The then and else clauses contain one statement each, but it can be a structured statement. For example,

if J <> 0 then

begin

  Result := I/J;

  Count := Count + 1;

end

else if Count = Last then

  Done := True

else

  Exit;

Notice that there is never a semicolon between the then clause and the word else. You can place a semicolon after an entire if statement to separate it from the next statement in its block, but the then and else clauses require nothing more than a space or carriage return between them. Placing a semicolon immediately before else (in an if statement) is a common programming error.

A special difficulty arises in connection with nested if statements. The problem arises because some if statements have else clauses while others do not, but the syntax for the two kinds of statement is otherwise the same. In a series of nested conditionals where there are fewer else clauses than if statements, it may not seem clear which else clauses are bound to which ifs. Consider a statement of the form

if expression1 then if expression2 then statement1 else statement2;

There would appear to be two ways to parse this:

if expression1 then [ if expression2 then statement1 else statement2 ];

if expression1 then [ if expression2 then statement1 ] else statement2;

The compiler always parses in the first way. That is, in real code, the statement

if ... { expression1 } then

  if ... { expression2 } then

    ... { statement1 }

  else

    ... { statement2 } ;

is equivalent to

if ... { expression1 } then

begin

  if ... { expression2 } then

    ... { statement1 }

  else

    ... { statement2 }

end;

The rule is that nested conditionals are parsed starting from the innermost conditional, with each else bound to the nearest available if on its left. To force the compiler to read our example in the second way, you would have to write it explicitly as

if ... { expression1 } then

begin

  if ... { expression2 } then

    ... { statement1 }

end

else

  ... { statement2 } ;

 

Topic groups

 

See also

Case statements

Structured statements: Overview

 

 

译文

 

If语句

 

if语句有两种形式:if...thenif...then...elseif...then的语法是:

if expression then statement

这里的表达式expression返回一个布尔值。如果表达式expression的值为真(True),那么语句statement被执行,否则不被执行。例如

if J <> 0 then Result := I/J;

if...then...else语句的语法是:

if expression then statement1 else statement2

这里的表达式expression同样返回一个布尔值。如果表达式expression的值为真(True),那么语句statement1被执行,否则语句statement2被执行。例如

if J = 0 then

  Exit

else

  Result := I/J;

thenelse子句各含有一个语句,这些子句可以是结构语句。例如

if J <> 0 then

begin

  Result := I/J;

  Count := Count + 1;

end

else if Count = Last then

  Done := True

else

  Exit;

注意,在then子句和保留字else之间决不要有分号(;)。为了将if语句与相同块的其他语句分开,可以在整个if语句之后放置一个分号(;),但在thenelse之间,除了允许有空格和回车换行之外,不需要任何其他任何标记(注释是允许的)。在if语句中的保留字else之前放置分号(;),是一般的编程错误。

连在一起的嵌套if语句也是需要特别注意。问题的出现是由于一些if语句有else子句而一些没有,但是两种语句在语法上又没有明显的区别。在嵌套的条件系列种,如果else子句比if语句少,那么else子句与if的搭配将不太清楚。对于具有如下形式的语句:

if expression1 then if expression2 then statement1 else statement2;

这里可以出现两种解析结果:

if expression1 then [ if expression2 then statement1 else statement2 ];

if expression1 then [ if expression2 then statement1 ] else statement2;

对于此类情况,编译器总是按照第一种方式解析。简言之,在实际的代码中,语句

if ... { 表达式1 } then

  if ... { 表达式2 } then

    ... { 语句1 }

  else

    ... { 语句2 } ;

等价于

if ... { expression1 } then

begin

  if ... { expression2 } then

    ... { statement1 }

  else

    ... { statement2 }

end;

 

上面的规则可以概括为,嵌套条件总是解析成从最内层的条件开始,对每个else总是与其左边最近的可用的if搭配。要强制编译器按照第二中方式读出前面的例子,就必需明确写成

if ... { expression1 } then

begin

  if ... { expression2 } then

    ... { statement1 }

end

else

  ... { statement2 } ;

 

主题组

 

相关主题

Case语句

结构语句:概述

 

 

编者注

要避免嵌套if语句产生歧义(明确地说,就是编写代码或阅读代码时人的理解与编译器根据规则作出的解析之间有偏差),可以通过尽量用if…then...else语句代替if...then...语句实现。(或者根据情况使用case语句,相关信息见Case语句中的编者注。)下面的两个例子就清楚地表达了代码的逻辑:

 

例子一:

if A then begin

  P1;

end else begin

  if B then begin

    P2;

  end else begin

    P3;

  end;

end;

 

例子二:

if A then begin

  if B then begin

    P1;

  end else begin

    P2;

  end;

end else begin

  P3;

end;

 

从上面两个例子可以看出,编写代码时,文本布局的风格也会影响到代码的可读性。例如,下面的语句读起来就比较费劲:

 

if A then

begin

  P1;

end

else

begin

if B then

begin

  P2;

end

else

begin

  P3;

end;

end;

 

尽管这里的代码与“例子一”的程序逻辑是一致的。