1

I m just trying to write my second (yes, it s absolutely new for me) teaching programm on Pascal. I ve made once, using "if", here it is:

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.

And how can I use case instead if for latest conditions?.. Can I write new Var-s , F, F2- Boolean and then somehow, making this:

F:= ((A=B) AND (C>D)=TRUE OR NOT A )  ;
F2:= ((TRUE<>A) AND (C-D<3)); 

use Case? It s really not easy for me, but hope, I can manage this task) sorry for my explanation. Thank you for attention

Еlena
  • 11
  • 1
  • Why do you think you want to use `case`? `case` is for multiple alternatives for a particular expression. In your situation, you have compound boolean expression. Your expressions can be simplified, too, because if you have a boolean variable, `A`, you don't need to check `if A = true`. That would be just `if A`. Likewise, `if A <> true` is the same as `if not A`. So `IF ((A=B) AND (C>D)=TRUE OR NOT A ) THEN IF ((TRUE<>A) AND (C-D<3))` becomes `IF ((A = B) AND (C > D)) OR NOT A AND A AND (C - D < 3)` which simplifies further to: `IF ((A = B) AND (C > D)) AND A AND (C - D < 3)` – lurker Nov 15 '15 at 20:18
  • thank you.. //this is it`s not me wish. Rather task. and conditions too. I can`t change them. I`ll try to understand) – Еlena Nov 15 '15 at 20:28
  • See http://www.freepascal.org/docs-html/ref/refsu52.html for a good demonstration of situations, where case is appropriate and how to use it. – jwdietrich Nov 16 '15 at 00:33

2 Answers2

0

There are several improvments possible to your use of boolean, but this is normal since you learn.

1

if (I= 1) then A:=true else A:=false;

You can directly assign to A the comparison result of I equal to 1

A := (I = 1);

Which leads to better byte code, even without optimization (a TEST than a SETZ instruction). The Paren pair is just for readability.

2

IF ((A=B) AND (C>D)=TRUE OR NOT A )

You don't need to compare the result of boolean operation to true of false.

if ((a=b) and (c>d) or not a)

The expression statements (a=b) and (c>d) already evaluates to a Boolean.

3

If you want to use the case of expression, let's say to replace the end of the program:

case (A = B) and (C > D)) and A and (C - D < 3) of
  true: writeln('yep');
  false: writeln('nope');
end;

note that I use the simplified statements of lurker comment.

Abstract type
  • 1,901
  • 2
  • 15
  • 26
0

As far as I understood you insisted on using a case statement. Though I entirely agree with @lurker and suppose that case statement is not useful here I have the following proposal.

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.

P.S. Check the logics of your if then else statements. There is a false decision: you decide that if not C > D then C < D. It is C <= D. I would advise you to read this SO answer as well.

Community
  • 1
  • 1
asd-tm
  • 3,381
  • 2
  • 24
  • 41