Examples of use - Loops & Conditions
This is an example that shows how to use the following steps:
- If Step
- Else If Step
- Else Step
- Loop Number Of Times Step
- While Loop
- Do While Loop
- For Each Loop
- Switch Step
- Switch Default Case Step
For this example, we will create one integer variable called IntVariable. We will use conditional statements, and depending on the value of IntVariable, the robot will execute different parts of the process.
Let's first create conditions steps. Every condition step must contain ONLY one "If Step" and ONLY one "Else Step". You can add as many "Else If Steps" as you want. A step group will be created for each condition where you can add other steps to execute if that condition is true.
We will create steps that check the value of the IntVariable and accordingly increase the variable by a certain number. Our rule will be:
- If IntVariable is lower than 3, increase IntVariable by 4. Let's now see in which cases will be executing this branch and what will be output:
- Else If IntVariable is less than or equal to 10, increase it by 5. This branch is executed if the condition in the "If step" is false and the condition in the "Else If step" is true, so when IntVariable is between [3, 10]
- Else the robot will increase IntVariable it by 6. This branch will robot execute only when "If Step" and "Else If Step" are false. In our case, this will be for any integer value that is bigger than 10. So, if our IntVariable is, for example 2,876 then we will go to Else branch and new value of IntVariable will be 2,882 (2,876 + 6).
NOTE: You can use conditional steps to compare decimal numbers, as well as for other uses such as checking if a string variable contains specific text.
We will now define our intVariable with value 5 and create the conditions we explained earlier.
If Step
In "If Step" you define your condition, and the robot will go to that branch if that condition is TRUE. In our case, the statement is IntVariable lower than 3.
Else If Step
In the "Else If Step" we will define that IntVariable must be lower or equal to 10. This step will be executed only if "If Step" is false, so when IntVariable isn't lower than 3. To achieve the same result, we can use the aforementioned condition: 3 <= *IntVariable* <= 10
Else Step
In "Else Step", there is no need to define a condition. This is a case where the robot will execute only when the "If Step" and all defined "Else If Steps" are false.
Let's take a look at how our editor will appear:
Could you tell me the value that our IntVariable will have after executing this process? You can find your results in our table of input and output parameters.
Input and Output variables:
Loop Number Of Times Step
Now we have an integer variable, IntVariable, with the value of 10. We will create new "Loop Number Of Times Step". For a number of loop iteration, we will use our IntVariable. As an index for loop iteration, we created a new variable called Index.
Under loop, we will use the "Increase Decrease Variable Step" to add IntVariable two at each loop iteration.
In the following table, you can find the values of the variables after each iteration.
When using a variable Index, it is only accessible within the loop it was created in. Once the loop is exited, the variable is no longer available.
While Loop / Do While Loop
If we want to perform a specific action based on a condition without knowing the exact number of iterations required, there are two types of loops you can use: "Do While Loop" and "While Loop". The only difference between these two loops is the order in which they check the condition. In "Do While Loop", the robot performs the action at least once and then checks the condition. On the other hand, in "While Loop", the robot first checks the condition and then enters the loop only if the condition is true. We will now use a "Do While Loop" with a condition while the IntVariable is bigger than 25. Within the loop, we will decrease the IntVariable by one. The initial value of the IntVariable before this loop is 30. As you can see, no iterators are present in this loop.
In the following table, you can find the values of the variables after each iteration.
Now we will create a new string variable, DemoVariable, with the value "this is demo variable".
We will now split the value of this variable to create an list of strings.
For Each Loop
Let's use a "For Each Loop" to read all values from the created list.
To better track the values in each loop iteration, we will now include a "Display State Step" where we will display "Iterator" variable.
In the following table, you can find the variable's value for each iteration.
After the loop is finished, the variable Iterator will hold the last value of the list, which in this case is "variable".
Switch Step
Now we will use "Switch Step". As an input parameter, the Switch step receives data from a variable. We will use our "Iterator" variable with the value of "variable".
Switch Default Case Step
When you add a "Switch Step", a "Default Case Step" will be automatically added. In this Step, you cannot add input or output variables.
Switch Case Step
To add a "Switch Case Step", click on the "+" button. In this step, you need to provide an input value as a case. For the input value, only a value can be added, not a variable. Let's add the value "this is my case" for our first case.
To add steps inside a case block, use the larger "+" button. Use the smaller "+" button to add another case.
We will add a step in the first case to increment the integer variable "Counter" by 1, which we have previously defined with a value of 4. Now we will add a second case with the value of "variable" and increment "Counter" variable by 2.
Our Switch Step now appears as follows:
If the variable defined in the Switch step is equal to "this is my case", then the variable "Counter" will have a value of 5. If the variable defined has the value "variable", then "Counter" will have a value of 6. In all other cases, the value will be undefined and it will be 4.
It is possible to add steps in the Default Case Step, but for now, we will skip that. If the value of the variable in the Switch Step is "variable", the second case step will be executed, and the "Counter" will have a new value of 6.