Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

for(? ; ? ; ?)

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi David

(*)

If it is not clear,please post again
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for all the responses.
d
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic