Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Breaking out of While Loop

    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 ...
    PHP Code:
    if($resultArray[0] AND $resultArray[1] AND $resultArray[2] AND $resultArray[3] AND $resultArray[4]) 
        {
          if(
    $resultArray[0]<=AND $resultArray[1]<=AND $resultArray[2]<=AND $resultArray[3]<=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

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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.

    PHP Code:
    $i=1;
    while(
    $result1<|| $result1<|| $result3<|| $result4<|| $result5<0){
        if(
    $result1>0){
        
    $resultToUse=$result1 ;
        }
        elseif(
    $result1<=AND $result2>0){
            
    $resultToUse=$result2;
        }
        elseif(
    $result1<=AND $result2<=AND $result3>0){
            
    $resultToUse=$result3;
        }
        elseif(
    $result1<=AND $result2<=AND $result3<=AND $result4>0){
            
    $resultToUse=$result4;
        }
        elseif(
    $result1<=AND $result2<=AND $result3<=AND $result4<=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]<=AND $resultArray[1]<=AND $resultArray[2]<=AND $resultArray[3]<=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

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Dunno about your problem, but noticed this

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

    Shouldn't that be

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

  • #5
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    PHP Code:
    $i 0;

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

        
    $i += 1;

    You have

    Code:
    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...

    Code:
    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{})

    PHP Code:
    do
    {

    } while (); 

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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...

    PHP Code:
    <?
    $a
    =0;
    $b=-2;
    $c='4';
        while(
    $a<|| $b<|| $c<0){
            echo(
    "line \n");
            if(
    $a<=AND $b<=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

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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.. :

    PHP Code:
    $i=0;
    while(
    $i<5){
        if(
    $result1>0){
        
    $resultToUse=$result1 ;
        }
        elseif(
    $result1<=AND $result2>0){
            
    $resultToUse=$result2;
        }
        elseif(
    $result1<=AND $result2<=AND $result3>0){
            
    $resultToUse=$result3;
        }
        elseif(
    $result1<=AND $result2<=AND $result3<=AND $result4>0){
            
    $resultToUse=$result4;
        }
        elseif(
    $result1<=AND $result2<=AND $result3<=AND $result4<=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

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    If you want a while loop that stops when all values in the array are 0, this is one way:

    PHP Code:
    $result = array(13234);
    $total 1;

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •