這一節描述基本語法中的流程語句: 條件語句 IF語句、 選擇語句 Case語句、循環語句 while/repeat/for、以及continue、break語句,還有終止程序
運行流程Exit、Halt方法。
廢話不多說,直接貼代碼。
{ Delphi語句
1、if語句
2、case語句
3、循環語句
4、用於循環的 continue 和 break 語句
5、程序終止或中止功能 Exit、Halt、Terminate方法
}
program Statement;
{$APPTYPE CONSOLE}
uses
SysUtils,
StrUtils, //引用字符串操作函數
Windows; //引用系統函數
type
charSet = set of ansichar;
var
//注意Delphi2010裡面String變量相當於WideString
dirPath:string;
dirAnsiPath:ansistring;
//定義一個整型變量
strVar:string;
nVar:integer;
//定義一個ansichar變量
chVar:ansichar;
charSetVar:charSet;
procedure showExit();
begin
WriteLn('Go into procedure showExit');
Exit();
//由於調用Exit()方法,因此會退出當前函數的執行,下面的語句不會被執行
WriteLn('Go out procedure showExit()');
end;
procedure showHalt();
begin
{
1、Halt可以返回一個錯誤碼,如果不帶參數則不能有 ( ) 函數調用符
2、如果用 ( ) 函數調用符,則必須帶參數
}
Halt(2);
end;
procedure showTerminate();
begin
//Terminate() 方法用於終止 GUI程序的執行,這裡就不說明啦
end;
begin
//通過系統API函數獲取系統路徑
GetWindowsDirectory(PWideChar(dirPath),256);
dirAnsiPath := dirPath;
dirAnsiPath := 'Window have install in' + dirAnsiPath;
WriteLn(dirAnsiPath);
{ IF語句
1、IF語句的第一種形式
2、如果有else分支,則then後面的 begin/end 語句塊 end後面不能有分號
3、如果有else分支,則then後面的語句必須為語句塊 begin/end
}
if dirPath = 'C:\windows' then
begin
WriteLn('Windows have install in default partion.');
end
else
begin
WriteLn('Windows have install in default partion.');
end;
{ IF語句
1、不帶esle子句的if語句
}
if True then
WriteLn('This is simple if statement');
{
if .... then
...
else if ... then
...
esle if ... then
...
else
...
}
//通過函數 Read 讀取數字
Read(nVar);
if nVar = 1 then
begin
WriteLn('status 1');
end
else if nVar = 2 then
begin
WriteLn('status 2');
end
else if nVar = 3 then
begin
WriteLn('status 3');
end
else
begin
WriteLn('other status');
end;
{ case 語句
}
case nVar of
1:
WriteLn('*');
2:
WriteLn('**');
3:
WriteLn('***');
4:
WriteLn('****');
else
WriteLn('Input is not 1..4');
end;
{ 循環語句
1、while循環
2、repeat循環
3、for循環
}
nVar := 0;
{
1、while循環語句
}
while not ( nVar = 10 ) do //注意 not的優先級比 關閉比較符的優先級要高
begin
Inc(nVar);
WriteLn(nVar);
end;
{ Repet
1、repeat循環語句,類似於C語言中的 Do...while,就是循環體至少會執行一次
}
repeat
WriteLn(nVar);
Dec(nVar);
until nVar = 0;
{ for循環語句
1、for有兩種形式,
2、語法格式1, 計數器向上增加
for 計數器 := 初始值 to 終值 do
循環體
3、語法格式2, 計數器向下減小
for 計數器 := 初始值 downto 終值 do
循環體
}
for nVar := 3 to 10 do
begin
WriteLn(nVar);
end;
for nVar := 10 downto 5 do
begin
WriteLn('This is downto for loop.');
WriteLn(nVar);
end;
{ 基於集合的for循環語句
1、針對集合中的元素進行循環
2、for I in set do 循環,中的set必須是已經初始化的集合變量
不能使集合類型,或者是未初始化的集合變量
3、這個格式的for循環還可以變量數組、記錄、字符串、類和接口
}
//初始化集合變量
charSetVar := ['a'..'z'];
for chVar in charSetVar do
begin
WriteLn(chVar);
end;
{ continue語句
1、continue語句的用法和 C語言中的用法一樣,用於中止本次循環
}
for nVar := 0 to 100 do
begin
if nVar <= 90 then
begin
Continue;
end
else
begin
WriteLn(nVar);
end;
end;
{ break語句
1、break語句與 C 語言中個的用法一樣
}
while nVar > 70 do
begin
Dec(nVar);
if nVar < 76 then
begin
break;
end
else
WriteLn(nVar);
end;
{ Exit 方法
1、Exit用於退出當前正在執行的程序塊,但是不會退出整個程序的執行
2、當Exit用於主程序的程序塊的時候就是退出程序
3、在try....finally...調用 Exit 會被當做異常處理
}
showExit();
WriteLn('In Main block.');
{ Halt 方法
1、Halt用於退出當前正在執行的程序
}
ShowHalt();
WriteLn('This statement can never be excuted.');
readln;
readln;
end.
歡迎轉載本系列文章,轉載請注明來源。