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 Java Objects (Retired) Harnessing the Power of Objects Incrementing and Decrementing

Jian Chen
Jian Chen
4,938 Points

what's the main difference between "if" and "while"?

The situation is to decrement as long as the dispenser is not empty. I don't understand why it does not work when I use "while" instead of "if" for: if (!isEmpty()) { mPezCount--;} . and Will using a do while loop work, for example: do {mPezCount--;} while (!isEmpty); also work? I would appreciate any help and clarification.

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey again Jian, the main different between and if statement, is that it only checks the object once. So if it sees that the pezDispenser is not empty by your code it will only decremented once.

The benefits of a while loop is that it will continue to check the conditional and continue to do the body of the statement until the while is no longer true, in this case: it will continue to decremented the PezDispenser until it is is empty.

Let's say we had 5 pez in the dispenser, you would need to run your if statement 5 times to make it decremented as much as a single while loop can, though this can be done it's easier to just use the while loop, who knows when you will be dealing with a value in the 1000's if not a 100,000.

In short, the while loop continues to check the the logic in your statement after the method is complete, where as an if method only performs the operation once.

Thanks! Let me know if this helps.

Jian Chen
Jian Chen
4,938 Points

But what allowed for the dispenser to perform 12 continuous decrements when Craig used "if" in the Incrementing and Decrementing video?

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Jian, that is correct, and Craig brought this up when he talked about 7:30 in his video, he wrapped the entire method he had into an while statement, in writing.

while (dispenser.dispense()) {
  System.out.println("Chomp")
}

When briefly discussing it Craig brought up that though we could run his if statement 12 times, it would be easier to wrap it in a while loop.

The entire point of the code above was to make sure the code ran while there was still pez in there without calling the dispense method on java 12 times.

I believe the main reason Craig used both loops is to give you introductions to both loops.

Thanks.

Jian Chen
Jian Chen
4,938 Points

okay Rob. I got it. Thanks for your help too!