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%
Results 1 to 20 of 25
Thread: Vote - Favorite Loop Statement
- 02-12-2009, 03:13 PM #1
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)
- 02-12-2009, 03:26 PM #2
ra ra ra for "for"
"for" is the loop I use the most... probably followed up by "while"
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-13-2009, 12:04 AM #3
you should add the special for loops.. forget name its not special but you know:
I know that they are for loops just better to use in array conditions, but they still are different.Java Code:int[] items = new int[12]; for(int item:items) { System.out.println(item); }
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.
- 02-13-2009, 12:12 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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".
- 02-13-2009, 03:10 AM #5
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:
Then I can change it to:Java Code:for(;true;){ // code here }
ORJava Code:while(true){ // code here }
AND don't you think this look messy??Java Code:for(int i=0;i<END;i++){ // code here }
compared to:Java Code:int i = 0 // code here while(i<END){ // code here i++; }
Java Code:int i=0 ... for(;i<END;i++){ // Beautiful!! }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-13-2009, 03:49 AM #6
yeah but check this out:
now compare to the Messy for loop:Java Code:while(input.hasNext()) System.out.print(input.next()); // BEAUTIFUL }
It all depends on situation. Or this scenario:Java Code:for(;;) { if(input.hasNext()) System.out.print(input.next()); else break; // MESSY }
compared to now Messy for loop OR while loop:Java Code:String[] array = { "Java", "For", "Loops", "Example", "String", "Array" }; for(String str:array) { // BEAUTIFUL Foreach System.out.print(str); }
Each different situation there is usually one best option to use, one loop that makes it cleaner code.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++; }Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-13-2009, 04:18 AM #7
well, this one should be written as:Java Code:for(;;) { if(input.hasNext()) System.out.print(input.next()); else break; // MESSY }
But yeah i get your point. The one i found myself using most often is for loop, wild while loop...rarely.Java Code:for( ;input.hasNext() ;) { System.out.print(input.next()); // STILL PRETTY }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-13-2009, 05:13 AM #8
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
what about this one )))
infinity is the best thing ))) make infinity of any loop )for(int i=0; i<1; i++){
i=0;
}
any more idea?Last edited by Webuser; 02-13-2009 at 05:15 AM.
- 02-13-2009, 05:17 AM #9
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.
- 02-13-2009, 05:18 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For me too, 'for' loo. Some cases for-each more reliable than 'for' loop to me.
- 02-13-2009, 05:21 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-13-2009, 07:14 AM #12
i wish this is possible.
where 15 can also be x of type int.Java Code:for(int i: 15){ // future of java?? list.get( i ); // SUPER PRETTY } // loop 15 times.
where list.size() == 50000.
instead of this.
EDIT:Java Code:for(int i=0; i<15; i++){ list.get( i ); // ALOT MORE TYPING... }
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)
- 02-13-2009, 08:04 AM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Okay, let me state it this way, I am just as comfortable using each of those loops.
- 02-13-2009, 08:14 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Each of those are useful in projects. ;)
- 02-14-2009, 04:52 AM #15
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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 acan take infinity if you write some like afor(int i=0; i<10000; i++){}
someplace in the loop body. It is more funny way than practical but it can be used too )))for(int i=0; i<10000; i++){i=0;}Last edited by Webuser; 02-14-2009 at 04:58 AM.
- 02-14-2009, 05:04 AM #16
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
[QUOTE=angryboy;56257]i wish this is possible.
but in which side? ++ or -- ? how you point it here? comes more like basic style I guess )))Java Code:for(int i: 15){ // future of java?? list.get( i ); // SUPER PRETTY } // loop 15 times.
- 02-14-2009, 05:43 AM #17
[QUOTE=Webuser;56475]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:
hehe :DJava Code:for(15) { // loop 15 times s = change(); // SUPER PRETTY System.out.println(s); // BEAUTIFUL }Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-14-2009, 07:02 AM #18
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-14-2009, 07:36 AM #19
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
For me, 'while' loop.
- 02-14-2009, 08:27 AM #20
Member
- Join Date
- Jul 2008
- Posts
- 67
- Rep Power
- 0
Similar Threads
-
Need help with a loop statement
By sunshine39 in forum New To JavaReplies: 7Last Post: 11-03-2008, 03:42 AM -
Least To Greates[ if statement and for loop]
By kris09 in forum New To JavaReplies: 1Last Post: 08-08-2008, 06:34 PM -
Beginner's Problem on Loop/If statement
By obdi in forum New To JavaReplies: 2Last Post: 07-07-2008, 01:41 AM -
Vote for my java bug
By kjanz1899 in forum Java AppletsReplies: 0Last Post: 06-29-2008, 05:29 PM -
Add Here Your Favorite Applets
By M77 in forum Java AppletsReplies: 4Last Post: 06-08-2008, 02:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks