...

View Full Version : Breaking out of While Loop



sir pannels
03-07-2007, 11:23 PM
Hi all,

Having some trouble breaking out of an While Loop.
I currently use two if statements, the first decides if all of the relavent array values have been set, if they have the second if statement then checks to make sure they are not all negative or zero. If they are all negative or zero, the while loop should end, with a break.

I use break 3; to break out of the two if statements and the while loop.

The code ...


if($resultArray[0] AND $resultArray[1] AND $resultArray[2] AND $resultArray[3] AND $resultArray[4])
{
if($resultArray[0]<=0 AND $resultArray[1]<=0 AND $resultArray[2]<=0 AND $resultArray[3]<=0 AND $resultArray[4]<=0){break 3;}
}


This is really ugly, if anyone knows of a better way then please tell me.

Anyway, it doesnt work. I have viewed a raw dump of the array and all the values are either negative or zero and yet the loop keeps going, it does not break. Which then crashes the browser haha.

If any one has any thoughts, it be much apperciated.

Warm Regards,
Sir P :D

marek_mar
03-07-2007, 11:35 PM
You can't break out of if statements. You an only break out of loop and switch statements.
If any of the array elements is 0 then the first if statement will fail.
Could you show us the whole loop?

sir pannels
03-07-2007, 11:40 PM
Ok I see, I misunderstood Break. So there is a way to achieve what I'm trying to get done here? Stop the while from running again if all are zero or less?

The whole loop, ok sure thing.. its quite long and I have not had time to clean it up yet. Please also ignore my comments I write in there for my benifit.



$i=1;
while($result1<0 || $result1<0 || $result3<0 || $result4<0 || $result5<0){
if($result1>0){
$resultToUse=$result1 ;
}
elseif($result1<=0 AND $result2>0){
$resultToUse=$result2;
}
elseif($result1<=0 AND $result2<=0 AND $result3>0){
$resultToUse=$result3;
}
elseif($result1<=0 AND $result2<=0 AND $result3<=0 AND $result4>0){
$resultToUse=$result4;
}
elseif($result1<=0 AND $result2<=0 AND $result3<=0 AND $result4<=0 AND $result5>0){
$resultToUse=$result5;
} //resultToUse works out first positive result


$final = 0;
if($result1<0){$final=$final - $result1;}
if($result2<0){$final=$final - $result2;}
if($result3<0){$final=$final - $result3;}
if($result4<0){$final=$final - $result4;}
if($result5<0){$final=$final - $result5;} // makes minus numbers positive (-5 becomes 5) and totals up

$part1=0;
if($result1>0){
$part1 = $resultToUse/$result1;
}

if($result2>0){
$part1b = $resultToUse/$result2;
$part1 = $part1 + $part1b;
}
if($result3>0){
$part1c = $resultToUse/$result3;
$part1 = $part1 + $part1c;
}
if($result4>0){
$part1d = $resultToUse/$result4;
$part1 = $part1 + $part1d;
}
if($result5>0){
$part1e = $resultToUse/$result5;
$part1 = $part1 + $part1e ; //divides resultToUse by all other positive results
}

$part2 = $final ; //total of minus numbers

if($i==1){$use=$result1;}
elseif($i==2){$use=$result2;}
elseif($i==3){$use=$result3;}
elseif($i==4){$use=$result4;}
elseif($i==5){$use=$result5;}

$part3 = $resultToUse / $use; //********CHANGE******
$part4 = $use; //******CAHNGE********
$result = $part4 - $part3 * ( $part2 / $part1) ;//********Changes
$resultArray[] = $result;
echo("<b>$result</b>");
if($resultArray[0] AND $resultArray[1] AND $resultArray[2] AND $resultArray[3] AND $resultArray[4]){
if($resultArray[0]<=0 AND $resultArray[1]<=0 AND $resultArray[2]<=0 AND $resultArray[3]<=0 AND $resultArray[4]<=0){break 3;}
}
print "<br>i is $i</br>";
$i++;
}// THIS IS THE END OF THE WHILE


what a mess. Anyway, any thoughts?

cheers for your time :)

Nightfire
03-08-2007, 12:49 AM
Dunno about your problem, but noticed this

while($result1<0 || $result1<0 |

Shouldn't that be

while($result1<0 || $result2<0 |

printf
03-08-2007, 02:36 AM
What are using while() for?

while() is condition based control, your condition(s) don't exist in it structure, so why use it.

Look at it this way


$i = 0;

while ( $i < 10 )
{
// loop ten times, $i is the condition that controlling the while

$i += 1;
}

You have


while ( $result1 < 0 || $result2 < 0 || $result3 < 0 || $result4 < 0 || $result5 < 0 )
{

}

The conditions are ( $result1, $result2, $result3, $result4, $result5 ), they don't get modified inside the control so why use while().

your real condition is this...


if($resultArray[0]<=0 AND $resultArray[1]<=0 AND $resultArray[2]<=0 AND $resultArray[3]<=0 AND $resultArray[4]<=0){break;}

so you should be using... (with the real condition in the while(); at then end of the do{})


do
{

} while ();

sir pannels
03-08-2007, 08:37 AM
Cheers nightfire, tidied that up...

printf, I get you, d'oh didnt have the conditions in the while.. I have changed that now to ...


while($resultArray[0]<0 || $resultArray[1]<0 || $resultArray[2]<0 || $resultArray[3]<0 || $resultArray[4]<0){

However I do not understand what you mean by using do and the while at the end of the do, could you explain? I dont see how the do would help break out of the while, or maybe i misunderstand do?

Many thanks :)

sir pannels
03-09-2007, 10:16 AM
Taking a look into this a bit more I built some code that does what i'm trying above... it runs a while loop taking away 1 from every variable as long as just one of the variables is <0 and as soon as they are all <= 0 it breaks the while and stops...



<?
$a=0;
$b=-2;
$c='4';
while($a<0 || $b<0 || $c<0){
echo("line \n");
if($a<=0 AND $b<=0 AND $c<=0){
break 1;
}
$a = $a-1;
$b = $b-1;
$c = $c-1;
}
?>


This works fine and returns the following.


line
line
line
line
line

So what am I doing wrong in my code? is it something to do with the fact im using array values in the if ?

Im stuck, any help apperciated.

Cheers all

sir pannels
03-09-2007, 11:03 AM
Ok forget all this, I think I can explain what I'm trying to do better...

I have 5 values stored in an array...
$resultArray[0] through $resultArray[4] ...

I want to run the following code while any of the above values are a negative number HOWEVER when they are ALL a negative number, i wish to stop the below code .. the code is I wish to run on the above conditions.. :



$i=0;
while($i<5){
if($result1>0){
$resultToUse=$result1 ;
}
elseif($result1<=0 AND $result2>0){
$resultToUse=$result2;
}
elseif($result1<=0 AND $result2<=0 AND $result3>0){
$resultToUse=$result3;
}
elseif($result1<=0 AND $result2<=0 AND $result3<=0 AND $result4>0){
$resultToUse=$result4;
}
elseif($result1<=0 AND $result2<=0 AND $result3<=0 AND $result4<=0 AND $result5>0){
$resultToUse=$result5;
} //resultToUse works out first positive result


$final = 0;
if($result1<0){$final=$final - $result1;}
if($result2<0){$final=$final - $result2;}
if($result3<0){$final=$final - $result3;}
if($result4<0){$final=$final - $result4;}
if($result5<0){$final=$final - $result5;} // makes minus numbers positive (-5 becomes 5) and totals up

$part1=0;
if($result1>0){
$part1 = $resultToUse/$result1;
}

if($result2>0){
$part1b = $resultToUse/$result2;
$part1 = $part1 + $part1b;
}
if($result3>0){
$part1c = $resultToUse/$result3;
$part1 = $part1 + $part1c;
}
if($result4>0){
$part1d = $resultToUse/$result4;
$part1 = $part1 + $part1d;
}
if($result5>0){
$part1e = $resultToUse/$result5;
$part1 = $part1 + $part1e; //divides resultToUse by all other positive results
}

$part2 = $final ; //total of minus numbers

if($i==1){$use=$result1;}
elseif($i==2){$use=$result2;}
elseif($i==3){$use=$result3;}
elseif($i==4){$use=$result4;}
elseif($i==5){$use=$result5;}

$part3 = $resultToUse / $use; //********CHANGE******
$part4 = $use; //******CAHNGE********
$result = $part4 - $part3 * ( $part2 / $part1) ;//********Changes
$resultArray[$i] = $result;
echo("<b>result$i = $result</b><br />");
print_r($resultArray);
print "<br>i is $i<br>";
$i++;
}

Hmm do I just wrap this in another while? I apperciate any assistance anyone can give me in achieving what I'm going for here.

Many thanks,
Sir P :D

aedrin
03-09-2007, 04:33 PM
If you want a while loop that stops when all values in the array are 0, this is one way:



$result = array(1, 3, 2, 3, 4);
$total = 1;

do {
// manipulate the values here
$total = array_sum($result);
} while ($total > 0);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum