Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for(? ; ? ; ?)

 
David Mroczkowski
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi David

(*)

If it is not clear,please post again
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for all the responses.
d
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic