Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Do While Loops

boolean

Hi there,

why does boolean always start with a false?

thanks!

Hi Eugene,

I'm not sure what you mean here - can you give me an example to assist?

Steve.

eg.

 int numberOfPutts = 0;
    boolean ballInHole = false;

    do{
      console.printf("Putts Putts %n");
      ballInHole = luck.nextBoolean();
      numberOfPutts ++;
    } while(!ballInHole);

     console.printf("You got it in %d putts %n", numberOfPutts);

[MOD: edited code block - srh]

1 Answer

Hi Eugene,

I don't think there's a conspiracy or a best-practice to initialize a boolean to hold a false value. Indeed, in that example code, the outcome of the loop is irrelevant to the initialization value - the boolean could have just been declared at the outset of the code; or set to true.

I'll happily stand corrected, but I don't think there's a best practice to set a boolean to a value of false as a matter of course.

Steve.