View Poll Results: Vote For Your Favorite Loop Statement:

Voters
11. You may not vote on this poll
  • For

    8 72.73%
  • For Each

    1 9.09%
  • While

    2 18.18%
  • Do While

    0 0%
Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default Vote - Favorite Loop Statement

    Please vote.
    Just wondering which is the most popular loop statments... :rolleyes:

    I use for loop statements most of the time.

    Also, there is a very good thread on loop performance here:
    loop performance (no problem, just investigating)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ra ra ra for "for"

    "for" is the loop I use the most... probably followed up by "while"

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    you should add the special for loops.. forget name its not special but you know:
    Java Code:
    int[] items = new int[12];
    for(int item:items) {
         System.out.println(item);
    }
    I know that they are for loops just better to use in array conditions, but they still are different.
    I voted for loops, but really they all have different purposes, some better at some things others better at other things, you don't just use your favourite. I am almost positive no one will vote do while. I have never seen any code before except maybe tutorial examples that has used a do while..
    EDIT: sorry didn't look carefully, didn't see for each... That's what its called.
    Last edited by MK12; 02-13-2009 at 12:06 AM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I'm sorry, but, whichever one is most applicable. And, the "enhanced" for loop is actually an Iterator and a while loop.

    If I have mulitple items to index into, with the same index, a for loop, if I have a single thing to iterate over, the enhanced for loop (so, a while loop). If I need to perform an action until a specific criteria is met, a while loop, unless I know, definately, that the first time will succeed, then a do while, etc, etc, etc.

    I have no "favorite".

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    ok... maybe "favorite" is a bad word choice on my part, but i'm sure most readers get what I've meant. Some of us have a loop statement they are more comfortable with. For me, it's the for loop, because everything's on one line.

    For example, during test:
    Java Code:
    for(;true;){
      // code here
    }
    Then I can change it to:
    Java Code:
    while(true){
      // code here
    }
    OR
    Java Code:
    for(int i=0;i<END;i++){
      // code here
    }
    AND don't you think this look messy??
    Java Code:
    int i = 0
    
    // code here
    
    while(i<END){
    
    
       // code here
    
    
       i++;
    }
    compared to:
    Java Code:
    int i=0
    ...
    for(;i<END;i++){
      // Beautiful!!
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    yeah but check this out:
    Java Code:
    while(input.hasNext())
          System.out.print(input.next()); // BEAUTIFUL
    }
    now compare to the Messy for loop:
    Java Code:
    for(;;) {
         if(input.hasNext()) System.out.print(input.next());
         else break; // MESSY
    }
    It all depends on situation. Or this scenario:
    Java Code:
    String[] array = { "Java", "For", "Loops", "Example", "String", "Array" };
    for(String str:array) { // BEAUTIFUL Foreach
         System.out.print(str);
    }
    compared to now Messy for loop OR while loop:
    Java Code:
    String[] array = { "Java", "For", "Loops", "Example", "String", "Array" };
    for(int i = 0; i < array.length; i++) {
         System.out.print(array[i]);
    }
    OR
    String[] array = { "Java", "For", "Loops", "Example", "String", "Array" };
    int i = 0;
    while(i < array.length) {
         System.out.print(array[i]);
         i++;
    }
    Each different situation there is usually one best option to use, one loop that makes it cleaner code.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Java Code:
    for(;;) {
         if(input.hasNext()) System.out.print(input.next());
         else break; // MESSY
    }
    well, this one should be written as:
    Java Code:
    for( ;input.hasNext() ;) {
       System.out.print(input.next());
       // STILL PRETTY
    }
    But yeah i get your point. The one i found myself using most often is for loop, wild while loop...rarely.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    what about this one )))

    for(int i=0; i<1; i++){
    i=0;
    }
    infinity is the best thing ))) make infinity of any loop )
    any more idea?
    Last edited by Webuser; 02-13-2009 at 05:15 AM.

  9. #9
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Webuser, I've always wondered, why do you always use right parentheses a lot like this )))?
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Webuser View Post
    what about this one )))
    Java Code:
    for(int i=0; i<1; i++){
    i=0;
    }
    infinity is the best thing ))) make infinity of any loop )
    any more idea?
    Why don't you use this way? Just few key-press you need.

    Java Code:
            for(;;) {
                // Do something here
            }

  12. #12
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i wish this is possible.
    Java Code:
    for(int i: 15){    // future of java??
      list.get( i );   // SUPER PRETTY
    }                  // loop 15 times.
    where 15 can also be x of type int.
    where list.size() == 50000.
    instead of this.
    Java Code:
    for(int i=0; i<15; i++){
      list.get( i );   // ALOT MORE TYPING...
    }
    EDIT:
    MiniQuiz: Do you think if this will work?
    Java Code:
    StringBuilder sb = new StringBuilder();
    Scanner input = new Scanner(new File("file.txt"));
    
    for(String s=input.next();input.hasNext();s=input.next()) {
      sb.append(s + " ");
    }
    System.out.println(sb);
    Last edited by angryboy; 02-13-2009 at 07:42 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Okay, let me state it this way, I am just as comfortable using each of those loops.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  15. #15
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Why don't you use this way? Just few key-press you need.

    Java Code:
            for(;;) {
                // Do something here
            }
    for more interest :) it is sick to use only standart ways )
    by the way in code like for( ; ; ){} you can predict the loop is endless at first sight but the way I showed can remake any for loop from limited to endless in unexpected manner ;)
    even code like a
    for(int i=0; i<10000; i++){}
    can take infinity if you write some like a
    for(int i=0; i<10000; i++){i=0;}
    someplace in the loop body. It is more funny way than practical but it can be used too )))
    Last edited by Webuser; 02-14-2009 at 04:58 AM.

  16. #16
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    [QUOTE=angryboy;56257]i wish this is possible.
    Java Code:
    for(int i: 15){    // future of java??
      list.get( i );   // SUPER PRETTY
    }                  // loop 15 times.
    but in which side? ++ or -- ? how you point it here? comes more like basic style I guess )))

  17. #17
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    [QUOTE=Webuser;56475]
    Quote Originally Posted by angryboy View Post
    i wish this is possible.
    Java Code:
    for(int i: 15){    // future of java??
      list.get( i );   // SUPER PRETTY
    }                  // loop 15 times.
    but in which side? ++ or -- ? how you point it here? comes more like basic style I guess )))
    And , what does i start at? once you put in these, you're back to the ordinary for loop. But for some cases, you don't need the i variable. consider this:
    Java Code:
    for(15) { // loop 15 times
         s = change(); // SUPER PRETTY
         System.out.println(s); // BEAUTIFUL
    }
    hehe :D
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  18. #18
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I use a plain old 'for' more often than anything else, followed by the new 'for each' construct {...for(String s: strings)...}, followed by 'while', and lastly 'do while'. Actually, I think I've only ever used do-while in c.

  19. #19
    7oclock is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    For me, 'while' loop.

  20. #20
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 03:42 AM
  2. Least To Greates[ if statement and for loop]
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 06:34 PM
  3. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM
  4. Vote for my java bug
    By kjanz1899 in forum Java Applets
    Replies: 0
    Last Post: 06-29-2008, 05:29 PM
  5. Add Here Your Favorite Applets
    By M77 in forum Java Applets
    Replies: 4
    Last Post: 06-08-2008, 02:49 AM

Posting Permissions

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