• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help !
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! And welcome to the Ranch!
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic