Condition "If ... then ..." (Statement "IF")
Please read the Terms of Use for Materials on ZennoLab
Table of contents
Description
The “ IF ” cube is one of the main logic actions used in ZennoPoster. In its work, it uses JavaScript if logic.
The “ IF ” action compares the values and, depending on whether they satisfy the condition or not, continues the action on the “green” or “red” branch. Boolean operators are used for comparison. In its simplest form, the if statement compares two values with each other and, if the value of the operator is correct, then the cube takes the value True , if the value is not true, then False .
Action properties is an input field where you can insert project variables, environment variables and constants: numbers or strings (text). Accordingly, the “ IF ” cube operates with only two types of data: numbers and strings.
What is it used for?
To compare the values of numbers
To compare text strings
To check conditions
How does the action work?
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 ( ! =
).
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
>
More
<=
Less than or equal
> =
More than or equal
In addition to these logical operators, in practice, complement operators are often used, which help to create more complex data comparison constructs.
OR operator (||)
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 .
Let's consider the operation of this operator using a specific example with text strings.
Both arguments are true (in the variable value1 the
value is different from “Hello“
, and in the variable value2
it is just the value “World“
) and therefore the cube ends with the green line.
The precedence of the AND operator &&
is more than the OR ||
, therefore, if both of them are present in the same cube, then the “AND” is executed first.
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.
We check the {-Page.Text-}
environment variable for emptiness and if it is empty (there is no text on the page), then the cube will come out in green layout and you can reload the page or put an additional pause while waiting for the page to load.
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).
Useful links