aspose file tools*
The moose likes Beginning Java and the fly likes for loop with one parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop with one parameter" Watch "for loop with one parameter" New topic
Author

for loop with one parameter

Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
hi,

I was wondering if someone could kindly explain me the running of this code.
public static void main(String[] args)
{
int i= 0;
for ( ;i<10 ; ){
System.out.println("Hello World!");
}
}

Rgds
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It will loop forever (as long as i is less than ten.)


[Jess in Action][AskingGoodQuestions]
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
Right. Thanks. i know it will. ok.. may be i should rephrase my question.
1)How does the for loop know what is the initial value of i (how does it actually know that it is i that it should be looking for). 2) When it says i<10, why does it print infinitely ? 3) when there is no iteration for the variable (i++) how/why does it iterate ?
infact, if I do this

there is no change to the output.
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Hi,
1) This for loop is not setting any initial value to anything, it's "initializer" part is empty.
2) The condition part tests the value of i, which is 0 according to the variable initialization. It prints infinitely because each time it checks whether i is less than 10, and nothing is changing the value of i, that's why you get an infinite loop.
3) It iterates because that's what a for construct does, whether you use the "increment" part or not. A for loop will only quit iteration when the condition is false (or a break is issued explicitly). Your for loop is exactly the same as: while(i < 10) { ... }

Finally, you can think of for( ; ; ) as an infinite loop because there is no condition to make it stop at all. But for( ; i<10 ; ) is infinite because the condition to stop is never met in that particular code sample. Keep in mind that the increment is not automatic, you have to make it explicit,e.g. for( ; i<10; i++ ).
[ September 26, 2007: Message edited by: greg buela ]

SCJP 1.5
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Mike Anna:
Right. Thanks. i know it will. ok.. may be i should rephrase my question.
1)How does the for loop know what is the initial value of i (how does it actually know that it is i that it should be looking for). 2) When it says i<10, why does it print infinitely ? 3) when there is no iteration for the variable (i++) how/why does it iterate ?
[/CODE]
there is no change to the output.


You need to read up on what the "for" loop really does. It seems to me like you may have only ever seen the commonest form of "for", which is something like this: -
[code]
for (int i=0; i < 123; ++i)
doSomething(i);
[/code]

In fact, "for" loops are very flexible. You can omit any, or all, of its three parts, with different effects. All sorts of weird loops can be constructed with "for". In practice, though, it is quite rare that there is a good justification for doing so; mostly, one should use the commonest form, keeping things simple for future maintenance.

Note that it's also fine to use the for-each form of the "for" loop. For example: -

[code]
int[] vals = new int[] { 1, 5, 99, -4, 33 };
for (int v : vals)
doSomething(v);
[/code]

Regarding your specific questions: -

(1) As you do not declare a variable called "i" in the "for" loop, it uses the varaible "i" that you declare just outside. That is initialised to 0.

(2) It prints indefinitely, because the condition for looping is "i < 10", but "i" will always be 0, as it is initialised to 0 and never changed.

(3) It's the second part of the "for" that controls whether looping continues, not the third. In your case, the second part is "i < 10", which is always true, so it always loops. Your "for" loop has no third part. If it did have a third part that was "i++", it would loop 10 times then stop.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
Got it !!
Thanks for answering. It is clear now. The exact reason, when I did a S.o.p for i, it always showed 0.
Read this somewhere in the meanwhile
All the sections in the for-header are optional. Any one of them can be left empty, but the two semicolons are mandatory. In particular, leaving out the <loop condition> signifies that the loop condition is true. The "crab", ( ; ; ) , is commonly used to construct an infinite loop, where termination is presumably achieved through code in the loop body (see next section on transfer statements):

for ( ; ; ) Java.programming(); // Infinite loop

The for-each loop is something I have never used.. will work on it now.

Thanks for your time everyone. Good day

[ September 26, 2007: Message edited by: Mike Anna ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop with one parameter