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!

Add new comment