Difference between revisions of "If/zh CN"

From Lazarus wiki
Jump to navigationJump to search
(New page: == If then(条件语句) == <delphi> if condition then statement1 else statement2; </delphi> Condition 是布尔表达式 如果 Condition 为真则执行 statement1,否...)
 
Line 17: Line 17:
 
<delphi>
 
<delphi>
 
   if condition
 
   if condition
     then statement1;
+
     then statement1;
 
     else statement2;
 
     else statement2;
 
</delphi>
 
</delphi>
Line 25: Line 25:
 
<delphi>
 
<delphi>
 
   if condition1
 
   if condition1
     then if condition2 then statement1;
+
     then if condition2 then statement1;
 
     else statement2;
 
     else statement2;
 
</delphi>
 
</delphi>
Line 38: Line 38:
 
   if condition
 
   if condition
 
     then begin  
 
     then begin  
           if condition2 then statement1;
+
           if condition2 then statement1;
 
           end
 
           end
 
     else statement2;
 
     else statement2;

Revision as of 06:29, 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