From Clomosy Docs

(Created page with "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.<br> Programming languages provide various control structures that allow for more complex execution paths.<br> A loop statement allows us to execute a statement or a group of statements multiple times.<br> <div class="table-responsive"> {| class="wikitab...")
 
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 21: Line 21:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
var
var
Line 34: Line 34:
  }
  }
}
}
</pre>
<b>Base Syntax</b><br>
<pre>
var
Counter: Integer;
begin
Counter := 1;
while Counter <= 5 do
begin
  ShowMessage('Counter: '+ IntToStr(Counter));
  Counter := Counter + 1;
end;
end;
</pre>
</pre>


<h2> For Loop </h2>
<h2> For Loop </h2>


A for statement, unlike a repeat-until, do-while or While statement, requires you to specify explicitly the number of iterations you want the loop to go through.<br>
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.<br>
The syntax of a for statement is:<br>
The syntax of a for statement is:<br>


Line 72: Line 58:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
var
var
Line 88: Line 74:
  }
  }
}
}
</pre>
<b>Base Syntax</b><br>
<pre>
var
Counter: Integer;
begin
for Counter := 1 to 5 do
begin
  ShowMessage('Counter: '+ IntTostr(Counter));
end;
for Counter := 5 downto 1 do
begin
  ShowMessage('Counter: '+ IntTostr(Counter));
end;
end;
</pre>
</pre>


Line 113: Line 82:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
var
var
Line 127: Line 96:
   }until (Counter > 5);
   }until (Counter > 5);
}
}
</pre>
<b>Base Syntax</b><br>
<pre>
var
num, sqrNum  : Integer;
begin
num := 1;
sqrNum := num* num;
Repeat
  ShowMessage(IntToStr(num)+' squared = '+IntToStr(sqrNum));
  Inc(num);
  sqrNum := num * num;
until sqrNum > 100;
end;
</pre>
</pre>


Line 169: Line 120:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
var  
var  
Line 186: Line 137:
  ShowMessage('Loop ended.');  
  ShowMessage('Loop ended.');  
}
}
</pre>
<b>Base Syntax</b><br>
<pre>
var
i: Integer;
begin
for i := 1 to 10 do
begin
  ShowMessage('Value: ' + IntToStr(i));
  // Exit the loop when i is 3
  if i = 3 then
  begin
    ShowMessage('Exiting the loop because i is 3.');
    Break;
  end;
end;
ShowMessage('Loop ended.');
end;
</pre>
</pre>


Line 211: Line 144:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
var
var
Line 229: Line 162:
   }
   }
}
}
</pre>
<b>Base Syntax</b><br>
<pre>
var
  i: Integer;
begin
  for i := 1 to 4 do
  begin
    // Skip this iteration when the value of i is 2 and move to the next iteration
    if i = 2 then
    begin
      ShowMessage('Skipping this iteration because i is 2.');
      Continue;
      // The statements after the Continue command are skipped
    end;
    // Displays i values other than 2 on the screen
    ShowMessage('In the loop, i = ' + IntToStr(i));
  end;
end;
</pre>
</pre>


Line 254: Line 168:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
var
var
Line 271: Line 185:
}
}
</pre>
</pre>
<b>Base Syntax</b><br>
{{#seo:|title=Loops in Clomosy - Clomosy Docs}}
<pre>
{{#seo:|description=Master Loops in Clomosy! Learn how to implement efficient loops for repetitive tasks in mobile app development with clear examples.}}
var
  i: Integer;
begin
  for i := 1 to 10 do
  begin
    ShowMessage('Value: ' + IntToStr(i));
    if i = 5 then
    begin
      ShowMessage('Exiting the loop because the value of i is 5.');
      Exit;
    end;
  end;
  ShowMessage('The loop has ended.');
end;
</pre>

Latest revision as of 11:31, 20 December 2024

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:

  • 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.

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.');
}