Break a For Loop

Asked

Viewed 1,529 times

1

How do I break a loop? EX:

for x:=1 to 10 do
if x = 5 then "break"
end

When it reached 5, the loop would be broken, no longer continue. Of course it would be another condition. I wonder if there’s a way.

  • 2

    The command is "break" right. Just take the quotes out of it and turn it into a command :-) if x = 5 then break;

  • I would not downvote this issue. Worse than this is not so obvious on Google looking in Portuguese, so I saw now. I even decided to turn my comment into a response.

2 answers

4

for x:=1 to 10 do
if x = 5 then break;
end

4


The command to break or break a loop, in Delphi, is break.

Using your code as an example:

for x:=1 to 10 do
begin
    if x = 5 then
        Break;
end;
x = 5; // nesta linha, x de fato será 5.

This command can be used to break any loop structure, such as while and repeat.

A command break breaks only the loop in which it is contained. If one loop is contained in another, the external loop will not be broken by the command break executed in the internal loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.