目录

if - then statement

if-then语句是最简单的控制语句形式,经常用于决策和更改程序执行的控制流程。

语法 (Syntax)

if-then语句的语法是 -

if condition then S

其中condition是布尔值或关系条件, S是简单或复合语句。 if-then语句的示例是 -

if (a <= 20) then
   c:= c+1;

如果布尔表达式condition计算结果为true,那么将执行if语句中的代码块。 如果boolean expression的计算结果为false,那么将执行if语句结束之后(结束结束之后;)的第一组代码。

Pascal假定任何非零和非零值为true,如果它为零或零,则假定为假值。

流程图 (Flow Diagram)

Pascal if-then声明

例子 (Example)

让我们尝试一个完整的例子来说明这个概念 -

program ifChecking;
var
{ local variable declaration }
   a:integer;
begin
   a:= 10;
   (* check the boolean condition using if statement *)
   if( a < 20 ) then
      (* if condition is true then print the following *) 
      writeln('a is less than 20 ' );
   writeln('value of a is : ', a);
end.

编译并执行上述代码时,会产生以下结果 -

a is less than 20
value of a is : 10
↑回到顶部↑
WIKI教程 @2018