Loops
Loops empower you to execute a sequence of code blocks multiple times, either a fixed number of times or until a certain condition is met. They streamline your code, making it more efficient and adaptable.
repeat times do
A programming construct that executes a specific block of code for a predetermined number of times.
Key points:
Repeat _ Times: Indicates the number of times the code block should be executed.
The do block won’t be executed if the given number of times is smaller or equal to 0.
Do: Specifies the action being repeated.
It is commonly used for
Iteration: Performing an action multiple times.
Data processing: Processing elements of an array or list.
repeat while do
Often referred to as a while loop, is a programming construct that repeatedly executes a block of code as long as a specified condition is true.
Key points:
The condition is checked before each iteration.
If the condition is true, the code within the loop is executed.
If the condition is false, the loop terminates.
There's a risk of infinite loops if the condition never becomes false.
repeat until do
A programming construct that repeatedly executes a block of code until a specified condition becomes true.
Key points:
The condition is checked at the end of each iteration.
The code within the loop is executed at least once.
The loop continues as long as the condition is false.
There's a risk of infinite loops if the condition never becomes false.
count with from to by do
A programming construct used to repeatedly execute a block of code a specific number of times.
Key points:
count variable (i in the example): Sets up the loop counter variable. The variable is automatically created.
from: Specify the start value of the variable.
to: Specify the end value. The end value itself is included. For example, in the example, the do statement will be executed 10 times.
by (Increment/Decrement): Updates the loop counter after each iteration. If the to value is larger than or equal to the from value, the variable is increased during each iteration, no matter whether the by value is positive or negative. If the to value is smaller than the from value, the variable is decreased during each iteration, no matter whether the by value is positive or negative.
for each item in list do
A programming construct used to iterate over each element in a collection (like an array or list).
Key points:
Iterates through each item in a collection.
Simplifies iteration compared to traditional for loops.
Often used with collections like arrays, lists, sets, or dictionaries.
The loop variable takes on the value of each element in the collection.
break out of loop
Used to prematurely terminate a loop.
Key points:
Immediately exits the current loop.
Skips the remaining iterations of the loop.
Common Use Cases:
Finding a specific value in a list or array.
Handling unexpected conditions that require immediate loop termination.
Optimizing loop performance by exiting early when a result is found.
continue with next iteration of loop
Used to skip the rest of the current iteration of a loop and proceed to the next iteration. How it works:
When encountered within a loop, the continue statement immediately terminates the current iteration.
The program flow jumps back to the beginning of the loop for the next iteration.
Key points:
Continue is often used in conjunction with conditional statements to skip certain iterations based on specific criteria.
It's different from break, which terminates the entire loop.
By using continue, you can efficiently process data or perform actions on specific elements within a loop while skipping others.
Can’t find it? You need to choose the “break out of loop” block, then choose “continue with next iteration” from the drop down menu.