Hi i have a loop and i was wondering if there was a command that you can jump back to the start of the loop and ignore the rest of the code in the loop
example:
for ($index = 0; $index < 10; $index++)
{
if ($index == 6)
that command to go the start of the loop
echo "$index, ";
}
should output
1,2,3,4,5,7,8,9 and skip the six
sort of same result as
for ($index = 0; $index < 10; $index++)
{
if ($index != 6)
echo "$index, ";
}
is there a command for that?
thanks, matthy
The keyword to use is continue
:
for ($index = 0; $index < 10; $index++)
{
if ($index == 6)
continue; // Skips everything below it and jumps to next iteration
echo "$index, ";
}
As an aside, to get your desired output your for
line should read this instead (unless you missed zero):
for ($index = 1; $index < 10; $index++)
Yes, continue
advances to the next iteration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With