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

I'm confused about while loops.

Hello, I am experiencing some confusion about while loops. I recognize that they have a do-->if-->while structure and I can write one, but I don't understand what's happening. Can anyone illuminate me? Please explain in simple terms. Thanks!

1 Answer

Bramyn Payne
Bramyn Payne
19,589 Points

Hello,

There are While Loops and Do-While Loops. In a While Loop, you specify the condition to be met, and if the condition is met (the statement evaluates to "true") the body of the loop executes. Then, the condition is checked again. If "true", the body of the code will execute; if "false", it will exit the While Loop.

A Do-While Loop is similar, but it first executes the body of the code. After execution, it checks the condition. If "true", the code will execute again. If "false", it will exit.

They are very similar, but the main difference is that a Do-While Loop will always run at least once. A While Loop does not have to run at all if the condition is initially "false".