Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
do-while Loops
The do-while loop is similar in function to the while loop, but the conditional test goes in a different place. The following is an example of a do-while loop:
do {
// the statements inside the loop go here
} while ( gameLives > 0 );
Like the previous while loop, this loop will continue looping until the gameLives variable is no longer greater than 0. The do-while loop is different because the conditional test is conducted after the statements inside the loop, instead of before them.
When the do loop is reached for the first time as a program runs, the statements between the do and the while are handled automatically. Then the while condition is tested to determine whether the loop should be repeated. If the while condition is true, the loop goes around one more time. If the condition is false, the loop ends. Something must happen inside the do and while statements that changes the condition tested with while, or the loop will continue indefinitely. The statements inside a do-while loop will always be handled at least once.
The following statements cause a do-while loop to display the same line of text several times:
int limit = 5;
int count = 1;
do {
System.out.println("Frodo lives!");
count++;
while (count < limit)
Like a while loop, a do-while loop uses one or more variables that are set up before the loop statement.
The loop displays the text Frodo lives! four times. If you gave the count variable an initial value of 6 instead of 1, the text would be displayed once, even though count is never less than limit.
If you're still confused about the difference between a while loop and a do-while loop, engage in a little role-playing and pretend you're a teenager who wants to borrow your father's car. If you are a teenager, all the better. There are two strategies that you can take:
- Borrow the car first and tell Dad later that you did it.
- Ask Dad before you borrow the car.
Strategy 1 has an advantage over Strategy 2: even if Dad doesn't want to let you use the car, you get to borrow it once.
The do-while loop is like Strategy 1 because something happens once even if the loop condition is false the first time while is encountered.
The while loop is like Strategy 2 because nothing will happen unless the while condition at the beginning is true. It all depends on the situation in your program.
Exiting a Loop | Next Section

Account Sign In
View your cart