Difference between revisions of "If/zh CN"

From Lazarus wiki
Jump to navigationJump to search
Line 61: Line 61:
  
 
{{Keywords}}
 
{{Keywords}}
 +
[[Category:zh]]

Revision as of 05:35, 5 December 2010

If then(条件语句)

<delphi>

 if condition
   then statement1
   else statement2;

</delphi>

Condition 是布尔表达式

如果 Condition 为真则执行 statement1,否则执行 statement2。

Else 和 statement2 可省略,此时如 Condition 为假则跳过 if 语句。

注意:1. else 之前不能加分号。下面是不合法的:

<delphi>

 if condition
   then statement1;
   else statement2;

</delphi>

2.else 总是与和它最近的if配对:

<delphi>

 if condition1
   then if condition2 then statement1;
   else statement2;

</delphi>


上面的else与第二个if配对;


要使else与第一个if配对,则应写成这样:

<delphi>

 if condition
   then begin 
          if condition2 then statement1;
         end
   else statement2;

</delphi>



If语句中使用复合语句

If语句可以使用复合语句 (begin ... end Block)。

<delphi>

 if boolean_condition then
   begin
     statement1;
     statement2;
     statement3;
   end;

</delphi>



Keywords: begindoelseendforifrepeatthenuntilwhile