Unnecessary if statement? Programming technique

From: Date: Tue, 06 Dec 2005 10:17:32 +0000
Subject: Unnecessary if statement? Programming technique
Groups: php.general 
Request: Send a blank email to php-general+get-226909@lists.php.net to get a copy of this message
Hi everyone Quick question: If I have such a loop: <? for($i=0;$i<1000000;$i++) { if($i==100) { // do something special for this occurence } // do something standard } ?> In this case it seems such a waste that the if() statement is done 999999 times when it's not needed. Is there any obvious trick that I am missing? I'm not sure how taxing a simple if() statement is on a server, maybe it's negligible, or is it something to worry about? Something which I'd prefer NOT to do: <? for($i=0;$i<100;$i++) { // do something standard } // do something special for $i = 100 for($i=101;$i<1000000;$i++) { // do something standard } ?> as I would have have to either keep two copies of the code or write a function just for this purpose, which hardly seems worth it. Thanks to anyone who takes the time to think about my question and/or respond. Best wishes, Steve McGill

« previous php.general (#226909) next »