The Do Statement

The 'do-while' loop is a variation of the while loop. 'do-while' loops always execute at least once, whereas while loops may never execute.

1. The Daring do Statement

Loops are so important, and are so common in programs, that there are several Java language features for implementing them. You have already seen while loops and for loops. This chapter looks at do loops.

A programming language needs to have only one general looping method. The while statement can be used to implement any type of loop. However, as we have seen, the for statement often is convenient, and also can be used to implement any type of loop.

The do loop is occasionally convenient and can be used to implement any of the three types of loops. However, of the three looping statements, it is used the least. Some programmers prefer not to use it at all.

The do statement is not part of the AP (Advanced Placement) computer science test given to high school students in the US. It is not used in the chapters that follow this one.

Chapter Topics:

      • The do statement
      • Bottom-driven loops
      • Dangers of bottom-driven loops

Question 1:

There are several looping statements in Java, although only one (any one of them) is logically necessary.

Are there also several branching statements in Java?          Is only one of them necessary?


Source: Bradley Kjell, http://programmedlessons.org/Java9/chap35/ch35_01.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.