# for loop

Sania Syed
Greenhorn
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
Posts: 4567
8
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
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
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
Posts: 6109
6
• 1
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
thanks for the help !

Jeff Verdegan
Bartender
Posts: 6109
6
You're welcome! And welcome to the Ranch!

Wendy Gibbons
Bartender
Posts: 1111
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
Posts: 15278
39
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.

fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
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.

Winston Gutkowski
Bartender
Posts: 10417
63
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

Campbell Ritchie
Sheriff
Posts: 48938
60
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.