The Type bool
The type bool has only two possible values:
true and false.
true and false are literal expressions.
Every literal expression is a primary expression.
Operator not
turns true into false and reverse.
Here is another simple example.
After running and studying this program, modify it so that
it outputs 'true' instead of 'false' when the
variable i equals 5 or 6.
The expressions on the left and right side of operator or are its operands.
Operator or
returns true when either of its operands
is true.
After running the program, modify it so that it also outputs
the result of expression 2<1 or 3<4.
After that, modify the program so that it outputs
the result of three more expressions:
- not false or true
- not (false or true)
- (not false) or true
Operator and
returns true only when both of its operands
are true.
The result of comparison operators is a value of type bool.
* * *
The expression not(7<>7) must use parentheses
because of the priority of operator not.
If this expression is written witout parentheses,
as not 7<>7,
then it is equivalent to (not 7)<>7,
which doesn't make any sense.
Normally, instead of writing not(7<>7),
one would simply write a simpler but equivalent expression
7=7.
The problem arises when a programmer wants to negate the expression
7=7, so he/she writes
not 7=7, which is incorrect.
This expression can be correctly written as not(7=7),
or simply as 7<>7.
Uncomment the erroneous line to see the error message. Then correct the error.
* * *
The inequality operator <> can be written in
an altentative way as !=.
The equality operator = can be written in
an altentative way as ==.
The isPressed function returns
a value of type bool.
Single quotation marks (') can be used instead
of double quotation marks (")
to delimit strings.
After running and examining the program, make it print out
the string The key "Escape" was pressed at the end of execution.
Run the new program, and then use the Escape key to end it.
After that, modify the program so that it also outputs a string
The function 'isPressed' returns a value of type 'bool'
at the end of execution.
Here is another example.
In this example, the value of variable b is true
when the F key is pressed. When the F
key is not pressed, the value of variable
b equals false.
The symbol ! can be used instead of
the keyword not.