This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop" Watch "for loop" New topic
Author

for loop

Sania Syed
Greenhorn

Joined: Oct 07, 2012
Posts: 3
I am trying to understand for loops and I wrote a simple program here



And the output is:

i=0
i=2
i=4
i=6
i=8

Why is it giving me all even numbers ? Shouldn't the output be

i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9

What am I doing wrong here ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4246
    
    7

Hi Sania. Welcome to the Ranch!

Why are you using i++ on line 6? That increments the value of i, which means it's being incremented twice each time round the loop.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Every time through the loop, you're doing i++ twice. Are you familiar with while loops? Your for loop is equivalent to this while loop.

Sania Syed
Greenhorn

Joined: Oct 07, 2012
Posts: 3
Oh I think I got what you mean so it should be like this




btw what exactly is the difference between for loop and while loop ? And, how would I know when to use for loop and while loop ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sania Syed wrote:Oh I think I got what you mean - so what exactly is the difference between for loop and while loop ?


Nothing, really. Any for loop can be turned into an equivalent while loop and vice versa.

For loops are traditionally used with a counter, when you're going to do something a specific number of times, and while loops are traditionally used when you don't know at the start of the loop how many iterations there will be and you're going to just keep going until some condition other than a simple counter is met. There's no execution advantage of one over the other, but if you start using them in non-standard ways, it can confuse people who read your code--including you.
Sania Syed
Greenhorn

Joined: Oct 07, 2012
Posts: 3
thanks for the help !
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome! And welcome to the Ranch!
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

the difference is being deterministic.
A for loop was designed to be deterministic, you can look at the top of the loop and know how many times it will run( i=0;i<10;i++) will run ten times

whereas all the others are not deterministic and you have to run through the code to find when they will exit.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Wendy Gibbons wrote:the difference is being deterministic.

That's not really true. Look at Sania's first post. The for-loop looks like for (i=0; i<10; i++) but still it executes only five times, not ten times.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Generally speaking, there is no difference. I believe all three loops (for, while, do-while) can all be interchanged with each other.

However, CONVENTIONS say that if you know exactly how many times a loop should run (i.e. iterating through a collection), a for-loop should be used. If you don't know how many times (i.e. validating user input, reading from a file, etc), a while loop should be used.

These aren't hard, fast rules, but most programmers would expect these to be followed, just like a class name should start with a capital letter.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Sania Syed wrote:I am trying to understand for loops...

There is another advantage of for loops, and that is that their syntax allows you to define variables entirely within the scope of the loop itself; so, for example, your loop could have (and probably should have) been written:and that's why I generally prefer them - sometimes even in cases where others might write a while loop.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Conventions (as Fred said) also provide usual formats for loops, etc. Some, but by no means all, of this thread discusses for loops and conventions, so it might be helpful to you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop
 
Similar Threads
New - initialization
Java: Thread
Passing Array references
Doubt in Thread
Exception -- java.lang.StackOverflowError