Panel | ||||||
---|---|---|---|---|---|---|
| ||||||
Please read the Terms of Use for Materials on ZennoLab |
Table of contents
Expand | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
|
...
Let's look at a simple example to understand this operator.
...
Create a variable
value1
and assign the value1
to it .Add an IF cube using the actions " Logic " → " If "
In the cube properties field, insert the operand
{-Variable.value1-}
Add the operator “ equals ” -
==
Let's insert the second value to compare - constant
1
.Now if you run the cube (select it and press the "Next" button in ProjectMaker), then the cube will end along the green line (a green check mark will appear). This tells us that the value is correct (True), since indeed the variable
value1
is1
.Change
value1
to2
in the “Variables” window and repeatstep
6. The cube will end along the red line (a red cross will appear). This tells us that the value is not correct (False), since the variablevalue1
is not equal to1
.
In the example above, we used numeric data and the equals ( ==
) comparison operator. In the next example, we will compare text data and for this we will use the not equal operator ( ! =
).
Warning |
---|
Important! When comparing text values, data and variables must be enclosed in quotation marks: single or double. Conversely, for numbers, quotes are not needed . Otherwise, the comparison will be incorrect. This is the mistake many newbies make. |
...
Create a variable
value1
and assign it the value“Hello World“
.Add an IF cube using the actions " Logic " → " If "
In the cube properties field, insert the operand
{-Variable.value1-}
Add the “ not equal ” operator -
! =
Let's insert the second value to compare - the constant
“Hello World“
.Now if you run the cube (select it and press the "Next" button in ProjectMaker), the cube will end with a red line (a red cross appears). And this is logical. The value in the variable is equal to the value of the second line and therefore the operator “ not equal to ”
! = Is
false and, accordingly, the die returns False .Let's change the
value1
toGoodbuy World
in the Variables window and repeatstep
6. The cube will end along the green line and this is absolutely true, because the string in the variable differs from the value on the right side of the expression and, accordingly, the operator “ not equal ”! = Is
true and the cube is returned by True .
In addition to the above operators, which are used to compare both numbers and strings, other operators are used to compare numbers:
<
Less
...
The OR operator looks like a double vertical slash. This operator allows you to compare several conditions in a chain at once. Moreover, they are calculated from left to right. The cube will return True if any expression is true, and False if all expressions are false.
...
Let's set the two variables
{-Variable.value1-}
and{-Variable.value2-} to
1
and12,
respectively.Add the line
{-Variable.value1-}> 0 || {-Variable.value2-} <10
Despite the fact that the second equality is not correct, the cube will end on the green line, because the OR operator will return True if at least one of the comparisons is correct.
Correct the value of the variable
{-Variable.value1-}
also to false (for example, assign the number 0) and then the cube will return False (the red branch will work).
...
AND operator (&&)
The AND operator is written as two ampersands &&
and returns True if all its arguments are true, otherwise - False .
...
Table of all available operators
Оператор | Описание | Пример использования |
---|---|---|
< | Less | 1<2 |
> | More | 5>3 |
<= | Less or equal | 7 <= 10 |
>= | More or equal | 8 >= 8 |
== | Equally | "Hello" == "Hello" |
!= | Not equal | “Hello”! = “Bye” |
&& | Logical "AND" | “Hello”! = “Bye” && 5>3 |
|| | Logical "OR" | "Day" == "Night"|| 2 > 1 |
...
Usage example
In practice, comparison is used very widely. For example, we need to check whether at least some text has loaded on the page or not.
...
Also, a frequent use of the “ IF ” cube is in cycles with a counter when a constant comparison of the counter with some maximum value is required - Cycles (loops).
...