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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    for loop vs while

    Hola folks, quick question regarding proper coding technique.
    I just wanted to know if for loops process any faster than while loops?

    Or is it okay to use while loops 99.9% of the time, such as l myself do?

    I was just wondering if there were any ramifications for practicing this.

    Samantha G.

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    for loops require setting another variable where as while loops often dont, this means they should be less straining on resources.

    Also while loops go until they aren't need any more and for loops you have to set how long they go for which means you may have to use a function such as count() or mysql_num_rows() to see how many times you want it to repeat which can also take processing time.

  • #3
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Well, l guess the main use l make of while loops is something like:

    PHP Code:
    $i=0;
    while(
    $i<$someNumber){
    //do something...

    $i++;

    So l'm using the while loops for 'generic' for loops, l just wanted to know if that's bad practice.

    Samantha G.

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    for() for me

    I just like them better, better to go around with and so

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    well a for loop is designed for that purpose so I guess it could be classed as bad practice.

    Using a for loop for the same example you showed cuts out 2 lines because you set the variable and increment it all in the 1 line. you could try using a microtime function to test exactly how much time the 2 methods take to do the same function.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    A for loop is a count controlled loop where as a while loop is a sentinel controlled loop. Although you could make a while loop act as a count controlled loop, that is not what it was designed for.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    There is almost no difference between those two:
    PHP Code:
    <?php
    while(condition)
    {
      
    // do something.
      
    $i++;
    }
    // Is the same as
    for( ; condition; )
    {
      
    // Do something.
      
    $i++;
    }
    // You could do something like this:
    while()
    {
      
    // do something
      
    if($condition)
      {
         break;
      }
      
    $i++;
    }

    for( ; ; )
    {
      
    // do something
      
    if($condition)
      {
         break;
      }
      
    $i++;
    }
    ?>
    For loops you keep the varables you need only inside the loop and expressions
    executed after each loop execution
    PHP Code:
    for($i 0$j 10$n count($someArray); $i $n$i++, $j--)
    {
       print 
    '$i = ' $i "\n" '$j = ' $j "\n";

    You could do all that with a while loop too
    PHP Code:
    $i 0;
    $j 10;
    $n count($someArray);
    while(
    $i $n)
    {
       print 
    '$i = ' $i "\n" '$j = ' $j "\n";
       
    $i++;
       
    $j--;

    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.

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Marek, your post is just repeating the other posts, and the code you gave in the first section was definately bad practice.

    She knows that both CAN be used she wanted to know IF it was bad practice to use a while loop instead of a for loop, which the answer is technically YES. For loops are designed for the purpose she has pointed out even tho while loops can do the same thing.

  • #9
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    While loops and for loops really have different uses. If your going to set a counting variable, (such as printing the first 10 of something) then it makes sence to use a for loop.
    PHP Code:
    for( $i=0$i<10$i++ ){
        echo 
    "blah";

    This can of course be done with a while loop, but it requires 2 more lines, (assuming you seperate your code as I do.)
    PHP Code:
    $i 0;
    while( 
    $i 10 ){
        echo 
    "blah";
        
    $i++;

    However, sometimes while loops are better than for loops. When dealing with MySQL results, with a for loop you would have to get the mysql_num_rows variable, however with a while loop, you do not:
    PHP Code:
    while( $row mysql_fetch_array($result) ){
        echo 
    "blah";

    vs
    PHP Code:
    $num mysql_fetch_array$result );
    for( 
    $x=0$x<$num$x++ ){
        echo 
    "blah";


    I hope this helps.
    If I'm postin here, I NEED YOUR HELP!!

  • #10
    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 use the for loop...
    PHP Code:
    for($i=0$row mysql_fetch_array$result ); $i++){ 
        echo 
    $i ': ' $row['something']; 

    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.

  • #11
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The point is though, it is bad practice.

  • #12
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Why would that be?
    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.

  • #13
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Refer to Ultragames post ^^

    Anyway, I think this thread is over, Samantha has her answer...I hope

  • #14
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In this day and age with processors becoming more and more powerful, performance is becoming less and less of a priority. While you should still take care not to overload the processor with redundant calls, you should be more concerned with the legibility of your source.

    PHP Code:
    $i 0;
    while (++
    $i 100)
    {

    Is it faster? It doesn't matter. It looks far more confusing than the structure it's emulating:

    PHP Code:
    for ($i 0$i 100$i++)
    {

    I'm not sure what your motivation for seeking an alternative is, but if it's to make your code run more quickly don't bother. Any gain in speed is going to be negligible unless you get into the hundreds-of-thousands of iterations.

  • #15
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It doesn't make sense SeeIT. Both loops are are almost identical and there is no real performance gain. Why would be using a for loop a bad practice?
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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