|
|
|
|
|
- Lesson 5 - Loops
- Overview
- Practical example
- The FOR statement
- The LOOP statement
- The WHILE statement
- To sum up
Tutorial - WLanguage basicsWe will cover the following topics: - The FOR statement.
- The LOOP statement.
- The WHILE statement.
15 min Loop statements make it possible to execute a process multiple times. A specific loop statement is used according to the number of iterations. Several statements can be used for loops: Practical example To complete the exercises in this lesson, we will work on the "WLanguage" project that we used in the previous lesson. The principle remains the same: the example code will be entered directly in the "Initialization" event of the project. To view WLanguage events of the project: - Right-click the "P" button next to the open element tabs. The context menu appears.
- Select "Element code".
- The code editor displays the different events associated with the project.
The FOR statement is used when the number of iterations is known. This statement uses a variable to control the iterations of a loop. The syntax of the FOR statement is as follows: FOR Index = Start value TO End value Process to run END To try this statement, we will enter a WLanguage code to fill an array of integers with even numbers between 1 and 10. We will then list the contents of the array. We will write this code in several steps. - If necessary, clear the "Initialization" event of the project.
- First of all, we will fill an array of integers with values from 1 to 10. Copy the following code:
ArrayOfNumbers is array of int
FOR Index = 1 TO 10
Trace("Loop | Index value: [%Index%]")
ArrayOfNumbers.Add(Index)
Trace("[%Index%] was added to the array")
END
This code executes the process 10 times and at each pass through the loop, the value of the index is stored in an array via the <Array>.Add function. - Let's test this code: click in the quick access buttons.
- If necessary, open the "Debugger trace" pane to see the result: on the "Home" tab, in the "Environment" group, expand "Panes", select "Panes", and then select "Debugger trace".
- The "Debugger trace" pane will contain the messages of the execution of the loop and the addition of the numbers in the array for the 10 indices.
Loop | Index value: 1 1 was added to the array Loop | Index value: 2 2 was added to the array Loop | Index value: 3 3 was added to the array Loop | Index value: 4 4 was added to the array Loop | Index value: 5 5 was added to the array Loop | Index value: 6 6 was added to the array Loop | Index value: 7 7 was added to the array Loop | Index value: 8 8 was added to the array Loop | Index value: 9 9 was added to the array Loop | Index value: 10 10 was added to the array
We will modify this code to exit the loop if the index is 5. In this case, we will check the value of the index and if it is 5, we can simply use the BREAK keyword to terminate the loop. - The code becomes:
ArrayOfNumbers is array of int
FOR Index = 1 TO 10
Trace("Loop | Index value: [%Index%]")
IF Index = 5 THEN
Trace("Break condition")
BREAK
END
ArrayOfNumbers.Add(Index)
Trace("[%Index%] was added to the array")
END
- Let's test this code: click in the quick access buttons.
- The "Debugger trace" pane will contain the messages of the execution of the loop and the addition of the numbers in the array for the first 5 indices.
Loop | Index value: 1 1 was added to the array Loop | Index value: 2 2 was added to the array Loop | Index value: 3 3 was added to the array Loop | Index value: 4 4 was added to the array Loop | Index value: 5 Break condition
Now, we will modify this code to add only even indices to the array. If the index is odd, the loop must skip on to the next iteration without adding a value to the array. We will use the CONTINUE keyword. - The code becomes:
ArrayOfNumbers is array of int
FOR Index = 1 TO 10
Trace("Loop | Index value: [%Index%]")
IF Index = 5 THEN
Trace("Break condition")
BREAK
END
IF IsOdd(Index) = True THEN
Trace("Odd index: go to next iteration")
CONTINUE
END
ArrayOfNumbers.Add(Index)
Trace("[%Index%] was added to the array")
END
- Let's test this code: click in the quick access buttons.
- The "Debugger trace" pane will contain the messages of the execution of the loop for the first 5 indices. For the odd numbers, a message indicates that the code is not executed. The addition message will only be displayed for the even numbers.
Loop | Index value: 1 Odd index: go to next iteration Loop | Index value: 2 2 was added to the array Loop | Index value: 3 Odd index: go to next iteration Loop | Index value: 4 4 was added to the array Loop | Index value: 5 Break condition
Now we will add the code to loop through the array in order to list the indices that have been added. We will use a FOR loop. The beginning and the end of the loop will be: - the first element of the array. This element corresponds to 1 (seen in lesson 3),
- the last element of the array. Since we don't know it, we will use the Count property. This property returns the number of elements in the array (and thus the index of the last element).
To loop through the array: - Add the following code:
FOR Index = 1 TO ArrayOfNumbers.Count
Trace("Index: [%Index%] | Value: " + ArrayOfNumbers[Index])
END
- This code can be optimized. With this syntax, the number of elements in the array is re-evaluated at each execution of the loop. If the number of elements in the array changes (e.g. some elements are deleted), the final value of the control variable will be adjusted. In our example, the array is not modified during the loop: it is possible to use the _TO_ keyword which avoids having to recalculate the number of iterations at each loop. The code becomes:
FOR Index = 1 _TO_ ArrayOfNumbers.Count
Trace("Index: [%Index%] | Value: " + ArrayOfNumbers[Index])
END
- Let's test this code: click in the quick access buttons.
- The "Debugger trace" pane will contain the messages displayed for the first 5 indices. For the odd numbers, a message indicates that the code is not executed. The contents of the array are then listed.
Loop | Index value: 1 Odd index: go to next iteration Loop | Index value: 2 2 was added to the array Loop | Index value: 3 Odd index: go to next iteration Loop | Index value: 4 4 was added to the array Loop | Index value: 5 Break condition Index: 1 | Value: 2 Index: 2 | Value: 4
Remark: You can define the step (index increment) value using the STEP keyword. For example, the following code runs the process 2000 times and the Index variable decreases by 10:
FOR Index = 2000 TO 1 STEP -10
Trace(Index)
END
The LOOP statement is used when the number of iterations is unknown. In this case, a condition must be evaluated to exit the loop. The syntax of the LOOP statement is as follows: LOOP Process to run IF <Condition to evaluate> THEN BREAK END We will write some WLanguage code to try this statement: - If necessary, clear the "Initialization" event of the project and copy the following code.
This code subtracts 1 from an integer until the value is zero:
Counter is int
Counter = 10
LOOP
Trace("Loop | Value: [%Counter%]")
Counter--
IF Counter = 0 THEN BREAK
END
Trace("End of loop")
- Let's test this code: click in the quick access buttons.
- The "Debugger trace" pane contains the message displayed for the 10 executions of the loop.
Loop | Value: 10 Loop | Value: 9 Loop | Value: 8 Loop | Value: 7 Loop | Value: 6 Loop | Value: 5 Loop | Value: 4 Loop | Value: 3 Loop | Value: 2 Loop | Value: 1 End of loop
The WHILE keyword can also be used in loops. In this case, the syntax of the LOOP statement is as follows:
Counter is int
Counter = 10
LOOP
Trace("Loop | Value: [%Counter%]")
Counter = Counter - 1
DO WHILE Counter > 0
Trace("End of loop")
The LOOP statement and the FOR statement can have the same behavior: all you have to do is use the syntax with exit according to the number of iterations: LOOP (<Number of iterations>) ... END In our example, the code can become:
Counter is int
Counter = 10
LOOP(10)
Trace("Loop | Value: [%Counter%]")
Counter --
END
Trace("End of loop")
For more details, see LOOP statement. The WHILE and LOOP statements follow the same principle. The difference is that the break condition is evaluated BEFORE the loop code is executed. This evaluation compares a variable to a given value. This variable starts at a starting value and is modified in the loop until it reaches the exit-condition. The syntax of the WHILE statement is as follows: <Initialize the variable to its start value> WHILE <Compare the variable to its end value> Process to run <Modify the variable> END We will write some WLanguage code to try this statement: - If necessary, clear the "Initialization" event of the project and copy the following code.
This code adds 1 to an integer as long as the integer is less than 10.
Counter is int = 1
WHILE Counter <= 10
Trace("Loop | Value: [%Counter%]")
Counter ++
END
Trace("End of loop")
- Let's test this code: click in the quick access buttons.
- The "Debugger trace" pane contains the message displayed for the 10 executions of the loop.
Loop | Value: 1 Loop | Value: 2 Loop | Value: 3 Loop | Value: 4 Loop | Value: 5 Loop | Value: 6 Loop | Value: 7 Loop | Value: 8 Loop | Value: 9 Loop | Value: 10 End of loop
In this lesson, you have discovered some of the conditional statements available in WLanguage: - The FOR statement.
- The LOOP statement.
- The WHILE statement.
In the next lesson, we will see how to create and use WLanguage procedures.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|