php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #17556 switch() and continue odd behaviour
Submitted: 2002-06-01 15:12 UTC Modified: 2003-01-02 19:58 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ahristov at icygen dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.2.1 OS: All
Private report: No CVE-ID: None
 [2002-06-01 15:12 UTC] ahristov at icygen dot com
 After 2 years of coding in PHP I found something strange to me but probably not to the creators of PHP :)). continue; inside a switch statement is equivalent of break; For this reason I've found 3 closed bugs - 5805, 8768 & 7591. However there is no info in the docs that switch() is one time loop - as said in one of the answer of one of the reports. I think that switch() docs has to mention that switch() is works like one iteration loop and that continue without any params within a switch is equivalent of break;. Thus the effect which one may want will be continue (level+1) (level is the level which will be if the program was written in C). Finally there should be short note in the continue; docs that inside a switch() it works somekind different than in C and provided link to switch() page for more info.

Best regards,
Andrey Hristov

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-01 17:02 UTC] jmcastagnetto@php.net
Ehem, the documentation for "switch" explicitly says:

"... The switch statement is similar to a series of IF statements on the same expression ..."

And I do not know of any language that uses a series of if statements as a loop construct (that is why for, while and do ... while are there). If you know of one, I will be interesting to check it out to see how that can be done.

Also, nowhere there is mentioned that "continue" can be used to loop in a non-looping construct (e.g. if), and if you read again the the description of "continue" it says:

"continue is used within looping structures to skip the rest of the current loop iteration and continue executio at the beginning of the next iteration"

thus if you use that in a context where there is no loop, it just gets out of scope.

This is (or should) something learned in basics of programming book, article, etc.

 [2002-07-29 10:11 UTC] ahristov at icygen dot com
I think that Mr Jesus Catagnetto misunsterstood what I am talking about. IMO the bug must be reopened.

I just wanted to say that
while (some){
 switch ($foo){
   case 'bar':
    if (some_reason) continue;//ie next iteration, but no, it will increment counter after the switch. Must be continue(2);
   break;
   case 'boo':
    //
   break;
   default:
   break;
 }
echo $counter++;
}
 [2002-07-29 16:47 UTC] jmcastagnetto@php.net
With respect to what you "wanted to say", your second example is not what you explained in your original report. Be more careful with your wording or include an example that clarifies your report. If you re-read your first report there is *no mention at all* of the case where switch is inside a loop (while, for, etc.)





Also, your logic does not seem too clear to me in your example. What is the *exact* intended effect?, perhaps that the "continue" goes to the top of the while loop  even though it is *not* in the current scope of that block but in the switch block, that will be an odd behaviour indeed. 





If you can be:





(a) more precise w/ respect to the execution flow, 





(b) provide some examples of other scripting languages where the switch supports a continue that behaves like you are mentioning here, 





(c) explain why such a construct is desirable instead of the more logic one of doing a break in the switch block and perfoming the conditional comparison outside the switch block, so you are not dependent on side-effects (which can be tricky to debug later on)





Then there will be a good change to reopening this bug. Will wait for your feedback, but will not reclassify this out of "Bogus" for the time being.



 [2002-07-30 04:12 UTC] ahristov at icygen dot com
Here are two examples:
<?php

$counter = 0;
while ($counter<20){
	switch ($counter){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			$counter++;
		break;
		default:
			$counter+=2;
			continue;
		
	}// swhitch
	echo 'FUBAR'."[$counter]\n";	
}//while

?>
<?php
$counter = 0;
while ($counter<20){
	switch ($counter){
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			$counter++;
		break;
		default:
			$counter+=2;
			continue(2);
	}// switch
	echo 'FUBAR'."[$counter]\n";	
}// while
?>
The intention is to show that there is no note in the docs for that when programmer wants to go to the next iteration of the loop that is shell to the switch() he/she must use continue(2); not continue;. This is because in switch()-es continue; is equivalent to break;
In general if programmer want to skip the rest of the n-th loop (counted from the most inner to most outter) he must use continue(n+1); and not contunie(n);

If there is something hard to get (beacuse of my english) please contact me and I will explain.
Regards,
Andrey Hristov
 [2002-07-31 18:53 UTC] sniper@php.net
reclassified.

 [2002-09-09 10:26 UTC] stas@php.net
Looks like docs problem, not Engine problem.
 [2003-01-02 19:58 UTC] nicos@php.net
http://www.php.net/manual/en/control-structures.switch.php looks now clear on that.

Thank for you report.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 19:01:28 2024 UTC