帕斯卡 - 布林



帕斯卡提供 Boolean 資料型別,使程式設計師能夠定義、儲存和處理邏輯實體,例如常量、變數、函式和表示式等。

布林值基本上是整數型別。布林型別變數有兩個預定義的可能值 TrueFalse。解析為布林值的表示式也可以分配給布林型別。

Free Pascal 還支援 ByteBoolWordBoolLongBool 型別。它們分別屬於型別 Byte、Word 或 Longint。

當轉換為布林值時,值 False 等於 0(零),任何非零值都被視為 True。如果布林值 True 分配給型別為 LongBool 的變數,則它將轉換為 -1。

需要注意,邏輯運算子 andornot 是為布林資料型別定義的。

布林資料型別的宣告

使用 var 關鍵字宣告布林型別的變數。

var
boolean-identifier: boolean;

例如,

var
choice: boolean;

例項

program exBoolean;
var
exit: boolean;

choice: char;
   begin
   writeln('Do you want to continue? ');
   writeln('Enter Y/y for yes, and N/n for no');
   readln(choice);

if(choice = 'n') then
   exit := true
else
   exit := false;

if (exit) then
   writeln(' Good Bye!')
else
   writeln('Please Continue');

readln;
end.

當編譯並執行以上程式碼時,它會生成以下結果 −

Do you want to continue?
Enter Y/y for yes, and N/n for no
N
Good Bye!
Y
Please Continue
廣告