Lesson #3. Boolean Expressions And Conditions
Conditions and branches
Conditions consist of comparison operators that produce the value of Boolean (True
or False
)
For example the comparison operand a > 5
:
If the value of left operand (in this case the value of variable a
) is greater than the right operand (number 5
) the condition becomes True
or else we get a False
:
... a = 6 a > 5 ←True
a = 5 a > 5 ←False
For example the comparison operand a >= 5
:
If the value of left operand is greater than and equal to 5
the condition becomes True
, or else we get a False
.
... a = 6 a >= 5 ←True
a = 5 a >= 5 ←True
Logic operations
Conditions may consist of logic operations: not
, or
, and
. If there are more than one condition, so each condition must be in round brackets.
For example for two conditions:
(year < 20) or (year > 18)
If one of the conditions or both conditions are True
then the whole condition is True
.
Or another example:
a = 5 (not a<4) and (7>5) ← True
The statements write
and print
can also return True
or False
:
... a = 5; write (a >= 5); // returns True ...
Tasks (conditions)
In this section you cannot use a conditional statement, you can use only write
or print
statement.
1. {0.5 points}[task-01-bool.pas]
Two integers are given. Check if the next statement is True: the first number is greater than the second (the program must return True
if it is true and False
otherwise).
2. {0.5 points}[task-02-bool.pas]
Two integers are given. Check the truth of the statement: the first number is not equal to the second. (the program must return True
if it is true and False
otherwise).
3. {0.5 points}[task-03-bool.pas]
Three integers are given: the values of variables A
, B
, C
. Check the truth of the double inequálity A < B < C
. Make sure that your program is correct with at least two input data sets, give the log of the program in the form of a comment.
4. {1 point}[task-04-bool.pas]
Three-digit integer is given. Check the truth: the first digit (left digit) of the number is less than the second (middle) and third (right).
5. {1 point}[task-05-bool.pas]
Two integers are given. Check the truth of the statement: at least one of these numbers is odd.
Note. Use the standard odd
function:
The function odd
returns True
when its argument is odd integer:
print (odd(5)); // true print (odd(6)); // falseexample:
-5, 8 >>> True 12, 0 >>> False 6, -1 >>> True 11, 7 >>> True