if
and while
are execution controllers that we will discuss in the next section. In this section we use the if
command to explain questions.
A simple example of a question:
x = 6 if x > 5 [ print "hello" ]In this example the question is the
x > 5
part. If the answer to this question is 'true' the code between the brackets will be executed. Questions are an important part of programming and often used together with execution controllers, like if
. All numbers and variables (number containers) can be compared to each other with questions.Here are all possible questions:
Table 4.1. Types of questions
a == b | equals | answer is “true” if a equals b |
a != b | not-equal | answer is “true” if a does not equal b |
a > b | greater than | answer is “true” if a is greater than b |
a < b | smaller than | answer is “true” if a is smaller than b |
a >= b | greater than or equals | answer is “true” if a is greater than or equals b |
a <= b | smaller than or equals | answer is “true” if a is smaller than or equals b |
Questions are highlighted with light blue in the code editor.
Question glue-words enable us to glue questions into one big question.
a = 1 b = 5 if (a < 5) and (b == 5) [ print "hello" ]In this example the glue-word
and
is used to glue 2 questions (a < 5
, b == 5
) together. If one side of the and
would answer “false” the whole question would answer “false”, because with the glue-word and
both sides need to be “true” in order to answer “true”. Please do not forget to use the brackets around the questions!Here is a schematic overview; a more detailed explanation follows below:
Table 4.2. Question glue-words
and | Both sides need to be 'true' in order to answer 'true' |
or | If one of the sides is 'true' the answer is 'true' |
not | Special case: only works on one question! Changes 'true' into 'false' and 'false' into 'true'. |
Question glue-words are highlighted with purple in the code editor.
When two questions are glued together with and
, both sides of the and
have to be 'true' in order to result in 'true'. An example:
a = 1 b = 5 if ((a < 10) and (b == 5)) and (a < b) [ print "hello" ]In this example you see a glued question glued onto an other question.
If one of the two questions that are glued together with or
is 'true' the result will be 'true'. An example:
a = 1 b = 5 if ((a < 10) or (b == 10)) or (a == 0) [ print "hello" ]In this example you see a glued question glued onto an other question.
not
is a special question glue-word because it only works for one question at the time. not
changes 'true' into 'false' and 'false' into 'true'. An example:
a = 1 b = 5 if not ((a < 10) and (b == 5)) [ print "hello" ] else [ print "not hello ;-)" ]In this example the glued question is 'true' yet the
not
changes it to 'false'. So in the end "not hello ;-)"
is printed on the canvas.
Would you like to make a comment or contribute an update to this page?
Send feedback to the KDE Docs Team