for loop with continue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
surya2183
Forum Newbie
Posts: 3
Joined: Fri Sep 18, 2009 12:13 am

for loop with continue

Post by surya2183 »

Hi to all,

Code: Select all

 
for ($i = 0; $i < 5; $i++){
    if ($i == 2)
        continue;
    print "$i<br />"; 
}
 
the above code outputs
0
1
3
4
but the "continue" definition in the php manual(http://www.php.net/manual/en/control-st ... ntinue.php) tells that the control will check the condition whenever it encounters the "continue" statement. According that definition control should check the condition then the program should output like
0
1
and $i value is 2(since control should go to check the condition) forever then the loop should become infinite...

But i thought that according for loop definition control will go to count variable and increments the $i value to 3 and checks the condition that is way it may give the result like
0
1
3
4
am I right? please clarify that the "continue" definition regarding for loop....
Thank you.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: for loop with continue

Post by requinix »

Your second explanation is right. It is, after all, the one that describes what actually happens.

Code: Select all

for (expr1; expr2; expr3)
Using continue in a for loop resumes execution at expr3 (the increment step) after which comes expr2 (the condition) and the body of the loop.

Code: Select all

    $i = 0 // expr1
loop_start:
    if $i < 2 then goto exit_loop // expr2
 
loop_body:
    if $i == 2 then goto loop_end
    print $i
 
loop_end:
    $i++ // expr3
    goto loop_start
 
exit_loop:
continue is the equivalent of "goto loop_end". Every looping structure is like this - while/until doesn't have an expr1 or expr3, foreach uses arrays, and switch doesn't have a "goto loop_start" in the loop_end segment.
surya2183
Forum Newbie
Posts: 3
Joined: Fri Sep 18, 2009 12:13 am

Re: for loop with continue

Post by surya2183 »

Thank you,
then why the PHP manual defines the continue definition like that. So they should change the "continue"(http://www.php.net/manual/en/control-st ... ntinue.php)definition regarding "for loop"
I think my opinion is right.is not it?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: for loop with continue

Post by requinix »

I think most people don't take what's written in the manual literally and trust experience over what a single webpage says :wink:

You can always submit a bug report at bugs.php.net...
davidcroda
Forum Newbie
Posts: 9
Joined: Wed Nov 08, 2006 10:36 am

Re: for loop with continue

Post by davidcroda »

surya2183 wrote:Thank you,
then why the PHP manual defines the continue definition like that. So they should change the "continue"(http://www.php.net/manual/en/control-st ... ntinue.php)definition regarding "for loop"
I think my opinion is right.is not it?
when they say "check the condition" they mean go back to the beginning of the for loop.

technically the "condition" is $i < 5, but the next thing executed is $i++, and the loop continues from there.
surya2183
Forum Newbie
Posts: 3
Joined: Fri Sep 18, 2009 12:13 am

Re: for loop with continue

Post by surya2183 »

Thank you....
Post Reply