From the course: Oracle Java Certification: 2. Operators and Decision Statements

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

While and do-while

While and do-while - Java Tutorial

From the course: Oracle Java Certification: 2. Operators and Decision Statements

Start my 1-month free trial

While and do-while

- Loop is another type of decision construct. Here is a simple while loop. How many times would this code print hello world? We have a loop variable called count. It is initialized to zero and the loop continues as long as count is less than 10. Because count is incremented at the end of each iteration, there are 10 different count values, zero through nine, that will cause the loop to continue. Therefore, the loop body will be executed 10 times, resulting in 10 hello worlds. Do while loop is similar to while loop. In this example, we attempt to convert the previous while loop to a do while loop. Is this implementation equivalent to the previous one? The key difference between a while loop and a do while loop is that a do while loop executes its loop body at least once, because the loop condition is checked after each iteration. It turned out that the code won't compile at all. A note that the variable i is actually…

Contents