Skip to main content

Do-While Statements


Introduction:
This page will teach you how to use the Do-While statements in Java.

Important!
It is fairly important you understand the While Statement before reading this tutorial page. It can be found on the main Java Tutorial Thread page.

What is a Do-While Statement?
A Do-While Statement is very similar to a While Statement, with one exception. The conditions that require to be true for the code to re-execute are checked at the bottom of the loop which therefore means the while script is executed at least once (before the first check).

Examples:

  1. boolean thisTrue = false;
  2. do {
  3. System.out.println("Executing...");
  4. }while (thisTrue);
  5. thisTrue = true;
  6. do {
  7. System.out.println("Executing...");
  8. }while (thisTrue);

The above code executes the output once while the boolean "thisTrue" is false, it then changes to true and outputs continuously. The output given was:

Executing...
	{Infinite Loop Starts Here}
Executing...
Executing...
Executing...
Executing...
	{Continues...}

Finished!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.