Pascal - 布尔值
Pascal提供了 Boolean 布尔值数据类型,使程序员能够定义、存储和操作逻辑实体,例如常量、变量、函数和表达式等。
布尔值基本上是整数类型。 布尔类型变量有两个预定义的可能值True和False。 解析为布尔值的表达式也可以分配给布尔类型。
Free Pascal 还支持 ByteBool、WordBool 和 LongBool 类型。 它们的类型分别是 Byte、Word 或 Longint。
值 False 相当于 0(零),任何非零值在转换为布尔值时都被视为 True。 如果布尔值 True 被分配给 LongBool 类型的变量,则该值将转换为 -1.
需要注意的是,逻辑运算符and、or和not是为布尔数据类型定义的。
布尔数据类型声明
使用 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

