布尔表达式
布尔表达式是由布尔运算符(如 and, or, not)和关系表达式组合而成的表达式,其结果也是一个布尔值(True/False)。
在以下的案例中:程序开始时,我们先将 x 设置为 True,y 设置为 False。
代码中的and 运算符要求两个操作数都为 True 时,结果才为 True。由于 x 是 True 而 y 是 False,所以 result 的值为 False,程序输出:x and y: False。
or 运算符只需一个操作数为 True,结果就为 True。在这里,x 是 True,所以 result 的值为 True,程序输出:x or y: True。
not 运算符用于取反。如果操作数是 True,则结果为 False;反之亦然。因此,not x 的结果是 False,程序输出:not x: False。
最后,我们将布尔表达式与关系运算符相结合。首先,5 > 3 和10 < 20这两个关系表达式分别被评估为 True,并赋值给x和 y。然后,使用 and 运算符对这两个 True 值进行逻辑与运算,结果自然是 True,程序输出:(5 > 3) and (10 < 20): True。
program tets;varx, y: Boolean;result: Boolean;beginx := True;y := False;result := (x and y); Writeln('x and y: ', result);result := (x or y); Writeln('x or y: ', result);result := not x; Writeln('not x: ', result);// 结合关系表达式x := (5 > 3);y := (10 < 20);result := (x and y); Writeln('(5 > 3) and (10 < 20): ', result);
end.

关系表达式
关系表达式用于比较两个值,并返回一个布尔值(True 或 False)。在Free Pascal 中,常见的关系运算符包括 =(等于)、<>(不等于)、<(小于)、>(大于)、<=(小于等于)和 >=(大于等于)。
program test;vara, b: Integer;result: Boolean;begina := 10;b := 20;result := (a < b); // TrueWriteln('a < b: ', result);result := (a = b); // FalseWriteln('a = b: ', result);result := (a > b); // FalseWriteln('a > b: ', result);result := (a <= b); // TrueWriteln('a <= b: ', result);result := (a >= b); // FalseWriteln('a >= b: ', result);result := (a <> b); // TrueWriteln('a <> b: ', result);
end.

if语言
if 语句用于根据条件执行不同的代码块。在 Pascal 中,if 语句可以包含 else if 和 else 子句。
program test;varnumber: Integer;beginWriteln('请输入一个数字:');Readln(number);if number < 10 thenWriteln('数字小于 10')else if number = 10 thenWriteln('数字等于 10')else if number > 10 thenWriteln('数字大于 10')elseWriteln('我知不道!');
end.

case语句
case 语句用于根据一个表达式的值选择执行多个可能的代码块之一。在 Pascal 中,case 语句与 of 关键字一起使用,并且每个分支都以:结尾,分支之间用;分隔。
program test;varday: Integer;beginWriteln('输入一个1-7之间的数字:');Readln(day);case day of1: Writeln('Monday');2: Writeln('Tuesday');3: Writeln('Wednesday');4: Writeln('Thursday');5: Writeln('Friday');6: Writeln('Saturday');7: Writeln('Sunday');elseWriteln('星期八!');end;
end.

