Logical expressions

A logical expression is a combination of boolean values or expressions (relational or logical ones) and logical operators.

The logical operators are: and, or, not.

and
or
not
intersection
union
negation

 

Logical and

The "and" expression returns true if the two terms are true, false otherwise.

Example of logical and: boolean x = true
boolean y = false
if x and y
   print "true"
else
   print "false"
/if
Displays: > true

 

Logical or

An "or" expression returns true if one of the two terms is true, false if the two ones are false.

Exemple de ou logique boolean x = true
boolean y = false
if x or y
   print "true"
else
   print "false"
/if
Displays: > false

 

Logical not

If we add the "not" operator on the whole expression, it negates the final result, thus: not(x or y) returns false.

Example of logical not boolean x = true
boolean y = false
if not (x or y)
   print "vrai"
else
   print "false"
/if
Displays: > false


If we associate "not" to one of the terms, it negates this term: x and not y as x is true, and y is false, thus not y is true the whole return true.

Example or unary not boolean x = true
boolean y = false
if x and not y
   print "true"
else
   print "false"
/if
Displays: > false

 

Implication

There is not keyword for the 'implication but it may be writtent with other keywords:

a implicates b is written:

not (a and not b)


 Exercises

 

1) To compose assortments of 3 chalks, a producer follows there rules.
- a white chalk when there is an amber one
- either a red or a white, at your choice.
- a green and an orange simultaneously.
Write a function named "compose", with three arguments x, y, z corresponding to colors of three chalks.
The function returns true or false according to the producer's rules.
Use the boolean variables w, r, a, g, o to indicates there a while, red, amber, green orange chalk.

Call the function with these values:
amber, white, red (returns true)
green, white, red (returns false)
orange, green, red (returns true)

Anwer

2) Write the thruth table for the implication formula.
For that, write a function "implication" with two boolean arguments, and which holds the formula given above.
Display the result for the 4 combinations of true and false.
Note that Php displays 1 or nothing, C++ displays true ou false.

Answer