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
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# for loop

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• 1
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
thanks for the help !

Jeff Verdegan
Bartender
Posts: 6109
6
• Number of slices to send:
Optional 'thank-you' note:
You're welcome! And welcome to the Ranch!

Bartender
Posts: 1111
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 13082
67
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 76863
366
• Number of slices to send:
Optional 'thank-you' note:
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.

 You are HERE! The other map is obviously wrong. Better confirm with this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising