Mittwoch, 13. Mai 2015

Boolean statements

Given variables a, b, c, d, e, f and g of type integer.
Which of the following  expressions are equal to:

if (a < b) and (c < d) then
    e := f
else
    e := g;

Only in case a is less than b AND c is less than d, and only then, e takes the value of f. Otherwise e takes the value of g.

[A]
e := f;
if not (a < b) then
    e := g;
if not (c < d) then
    e := g;

This can be changed to...

if not (a < b) or not (c < d) then
    e := g
else
    e := f;

...or...

if (a >= b) or (c >= d) then
    e := g
else
    e := f;

...which is option [B].

f is assigned to e only in case a < b and c < d. Otherwise e gets the value of g.
Therefore equal to the given term.  

[B]
if (a >= b) or (c >= d) then
    e := g
else
    e := f;

See option [A].

[C]
e := g;
if a < b then
    e := f;
if c < d then
    e := f;

Not equal. f is assigned to e in case a is less than b OR c is less than d.

if (a < b) or (c < d) then
    e := f
else
    e := g;

[D]
if a < b then
    if c < d then
        e := f
    else
        e := g;

Not equal. What value is assigned to e in case a >= b?

[E]
e := g;
if not (a >= b) then
    e := f;
if c < d then
    e := f;

Not equal.
if not (a >= b) can be changed to if a < b.

e := g;
if a < b then
    e := f;
if c < d then
    e := f;

Same as option [C].

Keine Kommentare:

Kommentar veröffentlichen