| Author |
for loop vs while loop
|
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
Hi,
What is the difference between for loop and while loop when we can get same result, why do we need second loop..What makes one loop better than the other one.
Output is:
x values is 0
x values is 1
x values is 2
y value is 0
y value is 1
y value is 2
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
For loop is better for iterating over indexed items like arrays...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
We can get the same functionality with while loop also..I may have to change the question. Which feature one loop has while the other loop doesn't have it. What I am trying to get is, Do we miss anything if we get rid of one of the loops from Java ?
|
 |
Ram Narayan.M
Ranch Hand
Joined: Jul 11, 2010
Posts: 244
|
|
Seconding Ankit's...
For loop is more suitable for iterating over arrays... If you use for loop for iteration of arrays, it will be catchy and understandable rather than while loop.
And While loop can be used in the context on which a loop has to be executed until certain condition is reached...
E.g.,
Reading contents from a file until EOF character is reached... Here while loop is apt one...
|
SCJP 6 [SCJP - Old is Gold]
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
There is no missing functionality in any of the loops. Its just that using for loop for iterating index elements is easier. Using while loop for the same functionality is possible but the code is ugly and harder to understand for anyone...
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
|
if you want to make a code that is more understandable for future developer who see your code and be thankful to you then use for loop
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56168
|
|
Shanky Sohar wrote:if you want to make a code that is more understandable for future developer who see your code and be thankful to you then use for loop
Not correct. Let me fix it:
"If you want to write code that is more understandable for both yourself and for future developers who see your code and be thankful to you, then use whichever loop type makes the most sense in the context of the code."
Sometimes the for loop will be the most readable, sometimes it will be the while loop. Use whechever makes the most sense -- but that's certainly not always the for loop.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Thanks for correcting...
But what i mean is for Array elements only...
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56168
|
|
Shanky Sohar wrote:But what i mean is for Array elements only...
Agreed, that is one of the purposes for which a for loop would most likely be the most appropriate choice.
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Thanks..........
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
if you want to keep on running a loop, until certain condition meet . then while loop is my choice rather than for loop.
For an example:
|
 |
Mina Daoud
Ranch Hand
Joined: Sep 24, 2010
Posts: 88
|
|
|
Use for loop if you know how many iterations you will need. Use while loop if you don't know how many iterations are required!
|
 |
Piotr Nowicki
Ranch Hand
Joined: Jul 13, 2010
Posts: 610
|
|
For loops should be generally prefered over while, as they are easier to understand and more intuitive.
AFAIR, Bruce Eckel suggested that the for loop should be also prefered because of the granularity of the variables. For loop lets you declare, define and use iteration variable only within the for, which conforms the choose-smallest-available-scope for the variable rule.
|
OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
|
 |
 |
|
|
subject: for loop vs while loop
|
|
|