Delphi学习笔记四——语句

来源:互联网 发布:知乎 旅游代购 吐槽 编辑:程序博客网 时间:2024/04/29 15:58

今天我们来看一下Delphi的语句。

一、常量声明语句

和其他语言一样,常量在声明时就被赋值,且在程序执行过程中是不可改变的。常量用“=”表示两边的值是相等的。

[delphi] view plaincopy
  1. const  
  2.       Pi = 3.14159;  
  3.       Answer = 342;  
  4.       ProductName = 'Delphi';  

 

二、赋值语句

这个语句最常用,在之前也介绍过。这里就举个例子。

[delphi] view plaincopy
  1. variable := expression;  

扩展方法{X+}:MyFunction(X)。这种方法被调用时,其返回值将会被丢弃。


三、goto语句

goto语句可以用来从程序中的一个地方直接跳转到另一个地方,但不常用。


四、复合语句

首尾使用begin和end包括起来的一组语句,可以嵌套使用,也允许空的复合语句。

[delphi] view plaincopy
  1. begin  
  2.     c:=a;  
  3.     a:=b;  
  4.     b:=c;  
  5.     begin  
  6.         ...  
  7.     end;  
  8. end;  

五、if语句

这个在日常的程序开发中也是经常用到。在之前的博文中也有介绍。这里不做详述,如有使用其他语言的经验,只需要注意Delphi对于if的语法即可。

[delphi] view plaincopy
  1. if  A then B  
  2. if  A then B else C  

举例来说:

[delphi] view plaincopy
  1. if J <> 0 then  
  2. begin  
  3.   Result := I/J;  
  4.   Count := Count + 1;  
  5. end  
  6. else if Count = Last then  
  7.   Done := True  
  8. else  
  9.   Exit;  

六、case语句

case语句用来在多个可能的情况下选择一个条件。例如

[delphi] view plaincopy
  1. case MyColor of  
  2.   Red: X := 1;  
  3.   Green: X := 2;  
  4.   Blue: X := 3;  
  5.   Yellow, Orange, Black: X := 0;  
  6. end;  
  7. case Selection of  
  8.   Done: Form1.Close;  
  9.   Compute: CalculateTotal(UnitCost, Quantity);  
  10. else  
  11.   Beep;  
  12. end;  

七、repeat语句

repeat语句重复执行一行或一段语句直到某一状态为真。例如

[delphi] view plaincopy
  1. repeat  
  2.   K := I mod J;  
  3.   I := J;  
  4.   J := K;  
  5. until J = 0;  
  6. repeat  
  7.   Write('Enter a value (0..9): ');  
  8.   Readln(I);  
  9. until (I >= 0and (I <= 9);  

这里需要说明的是,repeat至少执行一次语句

八、while语句

while语句与repeat语句的不同之处在于while语句在循环的开始阶段就进行判断,也就不存在多执行一次循环语句的情况了。

[delphi] view plaincopy
  1. while Data[I] <> X do I := I + 1;  
  2. while I > 0 do  
  3. begin  
  4.   if Odd(I) then Z := Z * X;  
  5.   I := I div 2;  
  6.   X := Sqr(X);  
  7. end;  
  8. while not Eof(InputFile) do  
  9. begin  
  10.   Readln(InputFile, Line);  
  11.   Process(Line);  
  12. end;  

九、for语句

for语句需要明确指定想要的循环来遍历的迭代次数:

[delphi] view plaincopy
  1. <p>for I := 2 to 63 do  
  2.   if Data[I] > Max then  
  3.     Max := Data[I];  
  4. for I := ListBox1.Items.Count - 1 downto 0 do  
  5.   ListBox1.Items[I] := UpperCase(ListBox1.Items[I]);  
  6. for I := 1 to 10 do  
  7.   for J := 1 to 10 do  
  8.   begin  
  9.     X := 0;  
  10.     for K := 1 to 10 do  
  11.       X := X + Mat1[I, K] * Mat2[K, J];  
  12.     Mat[I, J] := X;  
  13.   end;</p><p>for C := Red to Blue do Check(C);</p>  


十、Break过程

调用Break()表示当循环中满足某种条件时立即跳出循环。例如

[delphi] view plaincopy
  1. var  
  2.     i:integer;  
  3. begin  
  4.     for i:=1 to 100 do  
  5.     begin  
  6.         if i=30 then break;  
  7.     end;  
  8. end;  

十一、Continue()过程

调用Continue()重新开始下次循环。例如

[delphi] view plaincopy
  1. var  
  2.     i : integer;  
  3. begin  
  4.     for i:=1 to 3 do  
  5.          begin  
  6.              writeln(i,'before continue');  
  7.              if i=2 then continue;  
  8.             writeln(i,'after continue');  
  9.         end;  
  10. end;  

十二、with()语句

在使用记录类型的时候,使用with语句来针对某一个变量说明。with语句的结构为

[delphi] view plaincopy
  1. with obj do statement or with obj1,obj2,...,objn do statement  

with语句的例子

[delphi] view plaincopy
  1. with OrderDate do  
  2.   if Month = 12 then  
  3.   begin  
  4.     Month := 1;  
  5.     Year := Year + 1;  
  6.   end  
  7.   else  
  8.     Month := Month + 1;
0 0
原创粉丝点击