If statement 帕斯卡。如果

If statement 帕斯卡。如果,if-statement,case,pascal,If Statement,Case,Pascal,我只是想写我的第二个(是的,对我来说绝对是新的)关于Pascal的教学程序。我曾经用“如果”做过一次,这里是: program DemoBool; Var A: Boolean ; B: Boolean ; C:Integer ; D:Integer ; I:Integer; begin write('Enter A(1/0): '); readln(I); if (I= 1) then A:=true else A:=false; writeln(A); write('Enter B(

我只是想写我的第二个(是的,对我来说绝对是新的)关于Pascal的教学程序。我曾经用“如果”做过一次,这里是:

program DemoBool;
Var A: Boolean ; B: Boolean ;
C:Integer ; D:Integer ;
I:Integer;
begin
write('Enter A(1/0): '); readln(I);
 if (I= 1)
 then A:=true else A:=false;
 writeln(A);

write('Enter B(1/0): '); readln(I);
 if I=1 
 then B:=true else B:=false;
 writeln(B);

write('enter C: '); readln(C);
write('enter D: '); readln(D);

IF ((A=B) AND (C>D)=TRUE OR NOT A )
  THEN IF ((TRUE<>A) AND (C-D<3))
    THEN writeln('a=b, c>d, or not A=true, (true<>a and c-d<3)=true')
    ELSE writeln('a=b, c>d, or not A=true, (true<>a and c-d<3)=false')
ELSE writeln('a<>b, c<d, or not A=false') ;
readln;
end.
程序DemoBool;
变量A:布尔型;B:布尔型;
C:整数;D:整数;
I:整数;
开始
写入('输入一个(1/0):');readln(I);
如果(I=1)
然后A:=真,否则A:=假;
书面(A);
写入('输入B(1/0):');readln(I);
如果I=1
那么B:=真,否则B:=假;
书面(B);
写入('输入C:');readln(C);
写入('输入D:');readln(D);
如果((A=B)和(C>D)=真或假A)
那么如果((TRUEA)和(C-DD)=真或假A);

F2:=((TRUEA)和(C-D布尔值的使用可能会有一些改进,但这是正常的,因为您学习了

1. 您可以直接将
I
的比较结果指定给
A
等于
1

A := (I = 1);
这会产生更好的字节码,即使没有优化(比SETZ指令更适合测试)。Paren对只是为了可读性

2. 您不需要将布尔运算的结果与
false
true
进行比较

if ((a=b) and (c>d) or not a)
表达式语句
(a=b)和(c>d)
的计算结果已为布尔值

3. 如果要使用
表达式的大小写,我们假设替换程序的结尾:

case (A = B) and (C > D)) and A and (C - D < 3) of
  true: writeln('yep');
  false: writeln('nope');
end;
情况(A=B)和(C>D))以及
正确:writeln('yesp');
错误:书面形式('nope');
结束;

请注意,我使用了Louger comment的简化语句。

布尔值的使用可能会有一些改进,但这是正常的,因为您已经学会了

1. 您可以直接将
I
的比较结果指定给
A
等于
1

A := (I = 1);
这会产生更好的字节码,即使没有优化(比SETZ指令更适合测试)。Paren对只是为了可读性

2. 您不需要将布尔运算的结果与
false
true
进行比较

if ((a=b) and (c>d) or not a)
表达式语句
(a=b)和(c>d)
的计算结果已为布尔值

3. 如果要使用表达式的大小写,我们假设替换程序的结尾:

case (A = B) and (C > D)) and A and (C - D < 3) of
  true: writeln('yep');
  false: writeln('nope');
end;
情况(A=B)和(C>D))以及
正确:writeln('yesp');
错误:书面形式('nope');
结束;

请注意,我使用了潜伏者评论的简化语句。

据我所知,您坚持使用案例语句。虽然我完全同意@lower的观点,并且假设
case
语句在这里没有用处,但我有以下建议

program DemoBool;
var
  BitLogic: byte;
  tmpb: byte;
  A: Boolean;
  B: Boolean;
  C: Integer;
  D: Integer;
  I: Integer;

  function GetBoolBit(const B: Boolean; Offset: byte): byte;
  begin
    if B then
    begin
      result := 1;
      result := result shl Offset;
    end
    else
      result := 0;
  end;

begin
  //input the values

  BitLogic := GetBoolBit(A = B, 0);

  tmpb := GetBoolBit(A, 1);
  BitLogic := tmpb or BitLogic;//make comparison and store the result
  // in bit-by-bit mode - every new boolean with the new offset

  // the code is unscrolled here. We can optimize it writing the following instead:
  // BitLogic := GetBoolBit(A, 1) or BitLogic;

  tmpb := GetBoolBit(C > D, 2);
  BitLogic := tmpb or BitLogic;

  tmpb := GetBoolBit(C - D < 3, 3);
  BitLogic := tmpb or BitLogic;

  // we get the following the lower bit will store the first boolean - A=B
  // the second - the second - A
  // the third - C>D etc.
  // the fourth - C-D<3 etc.

  //now we can make an adequate case statement:

  case BitLogic of
    0: {00000000b}writeln('all false');
    1: {00000001b}writeln('A=B');
    2: {00000010b}writeln('A(=true)');
    3: {00000011b}writeln('A and A=B');
    4: {00000100b}writeln('C>D');
    5: {00000101b}writeln('C>D and A=B');
    6: {00000110b}writeln('C>D and A');
    7: {00000111b}writeln('C>D and A and A=B');
    8: {00001000b}writeln('C - D < 3');
    9: {00001001b}writeln('C - D < 3 and A=B');
    10:{00001010b}writeln('C - D < 3 and A');
    11:{00001011b}writeln('C - D < 3 and A and A=B');
    12:{00001100b}writeln('C - D < 3 and C>D');
    13:{00001101b}writeln('C - D < 3 and C>D and A=B');
    14:{00001110b}writeln('C - D < 3 and C>D and A');
    15:{00001111b}writeln('C - D < 3 and C>D and A and A=B');

  end;


end.
程序DemoBool;
变量
位逻辑:字节;
tmpb:字节;
A:布尔型;
B:布尔型;
C:整数;
D:整数;
I:整数;
函数GetBoolBit(const B:Boolean;Offset:byte):byte;
开始
如果B那么
开始
结果:=1;
结果:=结果shl偏移量;
结束
其他的
结果:=0;
结束;
开始
//输入值
位逻辑:=GetBoolBit(A=B,0);
tmpb:=GetBoolBit(A,1);
位逻辑:=tmpb或位逻辑//进行比较并存储结果
//在逐位模式下-具有新偏移量的每个新布尔值
//代码在这里展开。我们可以通过以下方式对其进行优化:
//位逻辑:=GetBoolBit(A,1)或位逻辑;
tmpb:=GetBoolBit(C>D,2);
位逻辑:=tmpb或位逻辑;
tmpb:=GetBoolBit(C-D<3,3);
位逻辑:=tmpb或位逻辑;
//我们得到以下结果,低位将存储第一个布尔值-A=B
//第二个-第二个-A
//第三个-C>D等。

//第四个-C-D然后是Ccase
语句在这里没有用处,但我有以下建议

program DemoBool;
var
  BitLogic: byte;
  tmpb: byte;
  A: Boolean;
  B: Boolean;
  C: Integer;
  D: Integer;
  I: Integer;

  function GetBoolBit(const B: Boolean; Offset: byte): byte;
  begin
    if B then
    begin
      result := 1;
      result := result shl Offset;
    end
    else
      result := 0;
  end;

begin
  //input the values

  BitLogic := GetBoolBit(A = B, 0);

  tmpb := GetBoolBit(A, 1);
  BitLogic := tmpb or BitLogic;//make comparison and store the result
  // in bit-by-bit mode - every new boolean with the new offset

  // the code is unscrolled here. We can optimize it writing the following instead:
  // BitLogic := GetBoolBit(A, 1) or BitLogic;

  tmpb := GetBoolBit(C > D, 2);
  BitLogic := tmpb or BitLogic;

  tmpb := GetBoolBit(C - D < 3, 3);
  BitLogic := tmpb or BitLogic;

  // we get the following the lower bit will store the first boolean - A=B
  // the second - the second - A
  // the third - C>D etc.
  // the fourth - C-D<3 etc.

  //now we can make an adequate case statement:

  case BitLogic of
    0: {00000000b}writeln('all false');
    1: {00000001b}writeln('A=B');
    2: {00000010b}writeln('A(=true)');
    3: {00000011b}writeln('A and A=B');
    4: {00000100b}writeln('C>D');
    5: {00000101b}writeln('C>D and A=B');
    6: {00000110b}writeln('C>D and A');
    7: {00000111b}writeln('C>D and A and A=B');
    8: {00001000b}writeln('C - D < 3');
    9: {00001001b}writeln('C - D < 3 and A=B');
    10:{00001010b}writeln('C - D < 3 and A');
    11:{00001011b}writeln('C - D < 3 and A and A=B');
    12:{00001100b}writeln('C - D < 3 and C>D');
    13:{00001101b}writeln('C - D < 3 and C>D and A=B');
    14:{00001110b}writeln('C - D < 3 and C>D and A');
    15:{00001111b}writeln('C - D < 3 and C>D and A and A=B');

  end;


end.
程序DemoBool;
变量
位逻辑:字节;
tmpb:字节;
A:布尔型;
B:布尔型;
C:整数;
D:整数;
I:整数;
函数GetBoolBit(const B:Boolean;Offset:byte):byte;
开始
如果B那么
开始
结果:=1;
结果:=结果shl偏移量;
结束
其他的
结果:=0;
结束;
开始
//输入值
位逻辑:=GetBoolBit(A=B,0);
tmpb:=GetBoolBit(A,1);
位逻辑:=tmpb或位逻辑//进行比较并存储结果
//在逐位模式下-具有新偏移量的每个新布尔值
//代码在这里展开。我们可以通过以下方式对其进行优化:
//位逻辑:=GetBoolBit(A,1)或位逻辑;
tmpb:=GetBoolBit(C>D,2);
位逻辑:=tmpb或位逻辑;
tmpb:=GetBoolBit(C-D<3,3);
位逻辑:=tmpb或位逻辑;
//我们得到以下结果,低位将存储第一个布尔值-A=B
//第二个-第二个-A
//第三个-C>D等。

//第四个-C-D D然后是Ccase
<代码>大小写用于特定表达式的多个备选方案。在您的情况下,您有复合布尔表达式。表达式也可以简化,因为如果您有一个布尔变量,
a
,则不需要检查a=true
。如果一个,那就是
。同样地,
如果是真的
如果不是
。所以
IF((A=B)和(C>D)=真或假A)那么IF((TRUEA)和(C-dd))或非A和A和(C-D<3)
进一步简化为:
IF((A=B)和(C>D))和A和(C-D<3)
谢谢//这是我的愿望。这是一项相当艰巨的任务。还有条件。我无法更改它们。我会尽力去理解)看看有没有一个很好的例子,在什么情况下case是合适的,以及如何使用它。你认为你为什么要使用
case
<代码>大小写
用于特定表达式的多个备选方案。在您的情况下,您有复合布尔表达式。表达式也可以简化,因为如果您有一个布尔变量,
a
,则不需要检查a=true。如果一个,那就是
。同样地,
如果真的
i相同