Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Why cant while replace for loop ???

  1. #1
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Why cant while replace for loop ???

    As far as my perception goes for loop is used to run a statement some specific number of time and while loop to run until a specific condition is met.

    but why cant while be like this and REPLACE the for loop

    Code:
    i=0;
    while(i<10)
    {
            code....
            ........
            i++;
    }
    i know the basics of python, c, java and php .. help me with these languages and please don bring in any other languages.

  2. #2
    Join Date
    Jul 2012
    Location
    Oklahoma, USA
    Beans
    Hidden!

    Re: Why cant while replace for loop ???

    Quote Originally Posted by vikkyhacks View Post
    As far as my perception goes for loop is used to run a statement some specific number of time and while loop to run until a specific condition is met.

    but why cant while be like this and REPLACE the for loop

    Code:
    i=0;
    while(i<10)
    {
            code....
            ........
            i++;
    }
    i know the basics of python, c, java and php .. help me with these languages and please don bring in any other languages.
    Why do you believe you can't?

    I would probably write it like this though...

    Code:
    var i = 0;
    
    while(i<10){
    code............;
    i++;
    }
    Who's Awesome? You're Awesome.

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,468
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Why cant while replace for loop ???

    while loop and for loop really are interchangeable

    Code:
    for( i=0; i<10; i++ )
    {
      ...
    }
    
    i=0;
    while( i<10 )
    {
      ...
      i++;
    }
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,160
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Why cant while replace for loop ???

    Quote Originally Posted by vikkyhacks View Post
    As far as my perception goes for loop is used to run a statement some specific number of time and while loop to run until a specific condition is met.

    but why cant while be like this and REPLACE the for loop

    Code:
    i=0;
    while(i<10)
    {
            code....
            ........
            i++;
    }
    i know the basics of python, c, java and php .. help me with these languages and please don bring in any other languages.
    To add to the other fine comments: it's also a matter of style. If you use a for loop you are implicitly telling readers that you are going to exhaustively process all elements in a list or all values in a range (unless you break early). Playing with the loop index outside the for statement will raise the WTF rating of your code. The while statement; by contrast, indicates that anything can happen to invalidate the exit condition, and that the index is incidental and isn't a taboo variable.

  5. #5
    Join Date
    Feb 2008
    Beans
    251
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Why cant while replace for loop ???

    In Python, the for loop is also very useful for processing the contents of a list in a sensible manner. consider the following examples

    Code:
    #!/usr/bin/env python
    
    my_list = ["a", "b", "c", "d", "e"]
    
    # for loop to process the elements of the list
    for item in my_list:
      print item
    
    
    # whereas to do the same with a while loop
    i = 0
    while i < len(my_list):
      print my_list[i]
      i+=1
    I think you'll agree that the for loop is "cleaner", and much better suited to this scenario. The code is much easier to read (by you, and third parties), which is a good practice to keep.

  6. #6
    Join Date
    May 2005
    Location
    Australia
    Beans
    24
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Why cant while replace for loop ???

    The for loop has the loop initializaion and incrementing on one line, which can make things a bit more readable because these statements aren't scattered around.

  7. #7
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Why cant while replace for loop ???

    Both the "for" and "while" loops have a place. As your experience grows, you'll probably find that in some situations, "for" is a more natural construct, and and in other places, "while" is more natural.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.

  8. #8
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Why cant while replace for loop ???

    thanks for the answers, are u telling me that just for clean code we have for loop and we can also write for programs using while loops WITHOUT USING FOR LOOPS in ALL CASES,

    no offence, but i just am not satisfied with any of the answers, did dennis and all other great minds bring for loop just for CLEAN CODE, isn't there some kind of code which only for loop can do !!!

  9. #9
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Why cant while replace for loop ???

    In coding there's always more than one way to skin a cat. It's that simple.

  10. #10
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,175
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Why cant while replace for loop ???

    Quote Originally Posted by vikkyhacks View Post
    thanks for the answers, are u telling me that just for clean code we have for loop and we can also write for programs using while loops WITHOUT USING FOR LOOPS in ALL CASES,

    no offence, but i just am not satisfied with any of the answers, did dennis and all other great minds bring for loop just for CLEAN CODE, isn't there some kind of code which only for loop can do !!!
    I'll talk C since that's what I know best of the 4 languages you list.

    C has 3 looping contructs, for, while, and do..while. This is a common feature of many languages. However, in C at least, any loop expressed in one of the 3 forms can also be expressed in both of the other forms, perhaps with the addition of extra variables, tests and braces. In fact, all loops can be expressed with if and goto.

    Some languages are more restrictive than C about how a for loop may be used, but so long as there remains a facility for an early exit from the loop (e.g. break, goto, return) then the looping
    constructs are probably completely interchangeable.

    The choice of construct is a matter of style; use whichever form seems most appropriate in the circumstances.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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