From Clomosy Docs
There may be situations where you need to execute a block of code multiple times. Typically, statements are executed sequentially: the first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complex execution paths.
A loop statement allows us to execute a statement or a group of statements multiple times.
Statement | Description |
---|---|
while loop | Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. |
for loop | Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
repeat-until loop | Like a while statement, except that it tests the condition at the end of the loop body. |
While Loop
A while statement is similar to a repeat statement, except that the control condition is evaluated before the first execution of the statement sequence. Hence, if the condition is false, the statement sequence is never executed.
Example
var Counter: Integer; { Counter = 1; while (Counter <= 5) { ShowMessage('Counter: '+ IntToStr(Counter)); Counter = Counter + 1; } }
For Loop
A for statement, unlike a repeat-until or While statement, requires you to specify explicitly the number of iterations you want the loop to go through.
The syntax of a for statement is:
for <variable> = <start> to <end>
{
// Actions to be taken
}
- variable: The variable that will change its value during the loop.
- start: The initial value of the loop.
- end: The final value of the loop. The loop terminates when this value is reached.
- to allows the loop variable to take values in an increasing order. This means it progresses upward from the starting value to the ending value.
- downto allows the loop variable to take values in a decreasing order. This means it progresses downward from the starting value to the ending value.
Example
var Counter: Integer; { for (Counter = 1 to 5) { ShowMessage('Counter: '+ IntTostr(Counter)); } for (Counter = 5 downto 1) { ShowMessage('Counter: '+ IntTostr(Counter)); } }
Repeat - Until Loop
Unlike for and while loops, which test the loop condition at the top of the loop, the repeat ... until loop in TRObject checks its condition at the bottom of the loop. The loop continues as long as the condition is false.
A repeat ... until loop is similar to a while loop, except that a repeat ... until loop is guaranteed to execute at least one time.
Example
var Counter: Integer; { Counter = 1; repeat { ShowMessage('Counter: '+ IntToStr(Counter)); Counter = Counter + 1; }until (Counter > 5); }
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Control Statement | Description |
---|---|
break statement | A command that immediately stops the execution of the loop and allows exiting from the loop. |
continue statement | It is a statement that allows the loop to skip the current iteration and proceed to the next iteration. |
exit statement | Although it is not a loop control statement, when used within a loop, it will terminate the loop and exit the local method in which it is found. |
Break Statement
The break statement is a command that immediately stops the execution of loops and allows exiting the loop. This statement is used to terminate the loop when a certain condition is met during its execution.
If a break statement is encountered within a loop, that loop will terminate immediately, and the program will continue executing from the point immediately following the loop. It can also be used in conditions outside of the loop.
Example
var i: Integer; { for (i = 1 to 10) { ShowMessage('Value: ' + IntToStr(i)); // Exit the loop when i is 3 if (i == 3) { ShowMessage('Exiting the loop because i is 3.'); Break; } } ShowMessage('Loop ended.'); }
Continue Statement
It is an expression that allows the current iteration of the loop to be skipped and proceeds to the next iteration. When a specific condition is met within the loop, it skips the current iteration of the loop and moves on to the next one. This is used to skip certain operations or to avoid executing the remaining statements of the loop when a condition is met.
Example
var i: Integer; { for (i = 1 to 4) { // Skip this iteration when the value of i is 2 and move to the next iteration if (i == 2) { ShowMessage('Skipping this iteration because i is 2.'); Continue; // The statements after the Continue command are skipped } // Displays i values other than 2 on the screen ShowMessage('In the loop, i = ' + IntToStr(i)); } }
Exit Statement
Although it is not a loop control statement, when used within a loop, it will exit both the loop and the local method it is in. Unlike the Break statement, the code lines following the loop in the local method cannot be executed either.
Example
var i: Integer; { for (i = 1 to 10) { ShowMessage('Value: ' + IntToStr(i)); if (i == 5) { ShowMessage('Exiting the loop because the value of i is 5.'); Exit; } } ShowMessage('The loop has ended.'); }