File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes for(? ; ? ; ?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for(? ; ? ; ?)" Watch "for(? ; ? ; ?)" New topic
Author

for(? ; ? ; ?)

David Mroczkowski
Greenhorn

Joined: Nov 07, 2002
Posts: 12
I came across a practice test question that had this in the for loop statement. I thought this would not compile. Am I wrong?
int i = 0;
for ( method1(i) ; method1(i); method2(i)) {
// code
}
boolean method1(int i) { i++ ;
return true;
}
boolean method2(int i) {i++;
return !(method1(i));
}
system.println(i);
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
That should compile. The only part that really matters is the test or second statement in the for statement, which must return boolean or null;


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

check out the JLS about how a for loop can look:
(�14.13) The for Statement
Basically you can (optionally) have any statement or list of statements in the 1st and 3rd spots... the only real requirement is that the 2nd spot evaluate to a boolean.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi David

(*)

If it is not clear,please post again


Francis Siu
SCJP, MCDBA
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[MM]: The only part that really matters is the test or second statement in the for statement, which must return boolean or null;
The test (second part of the for loop) must return boolean, not null.


"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
The test (second part of the for loop) must return boolean, not null.
What I meant was for (method1(); ; method1()) {. But yea, technically I suppose that is not null.
David Mroczkowski
Greenhorn

Joined: Nov 07, 2002
Posts: 12

Thanks for all the responses.
d
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
What I meant was for (method1(); ; method1()) {. But yea, technically I suppose that is not null.
Just to clarify. The compiler inserts an implicit boolean value of true into the test of a for loop when it is left empty. I should have remembered that from the Programmer Certification.
Here's an example:

Here's the disassembly of the above two methods:

Note that they are absolutely identical indicating that the compiler places the true value for you when you leave it empty. One further nuance to note, even though it is syntactically correct, if you place the literal false in the test portion, you will get a code unreachable compile-time error.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by David Mroczkowski:
I came across a practice test question that had this in the for loop statement. I thought this would not compile. Am I wrong?
int i = 0;
for ( method1(i) ; method1(i); method2(i)) {
// code
}
boolean method1(int i) { i++ ;
return true;
}
boolean method2(int i) {i++;
return !(method1(i));
}
system.println(i);

As others have answered, the for loop doesn't cause any compiler errors, but the issues with static and member methods will. Also, there is no class declaration, which will definitely cause a compiler error. Of course, it is probably safe to assume that this code IS inside a class, however, that brings the question of where the println() method call belongs. If we make some modifications, we can get it to compile and clarify some assumptions:

Also assuming an appropriate main() method that invokes this code, the next question is how does it act?
Since i is local and passed to various functions, its value never changes within the doIt() method. Also, method1() always returns true. This leads me to the conclusion that the for loop will repeatedly print a "0" to the console. In fact, it looks like an infinite loop to me, with the modifications made.
I guess this all hinges on the assumptions I stated earlier. If these are not similar to the actual code example, then please post the full code as it is supposed to be. At the very least include the class declaration and put all executing code into a method.
With all that said, the code as-is will NOT compile. Some assumptions and modifications need to be made to do so.
Regards,
Layne


Java API Documentation
The Java Tutorial
 
Don't get me started about those stupid light bulbs.
 
subject: for(? ; ? ; ?)