importantData.txt
/*
* DISCLAIMER: Neither the author nor John Wiley & Sons,
* Inc., nor anyone else even remotely connected with the
* creation of this book, assumes any responsibility
* for any damage of any kind due to the use of this code,
* or the use of any work derived from this code,
* including any work created partially or in full by
* the reader.
*
* Sign here:_______________________________ */
* Sign here:_______________________________
*/
import java.io.File;
import java.util.Scanner;
class IHopeYouKnowWhatYoureDoing {
public static void main(String args[>) {
Scanner keyboard = new Scanner(System.in);
char reply;
<strong> </strong>
<strong>do {</strong>
<strong>System.out.print("Reply with Y or N…");</strong>
<strong>System.out.print(" Delete the importantData file? ");</strong>
<strong>reply = keyboard.findWithinHorizon(".", 0).charAt(0);</strong>
<strong>} while (reply != 'Y' && reply != 'N');</strong>
if (reply == 'Y') {
new File("importantData.txt").delete();
System.out.println("Deleted!");
} else {
System.out.println("No harm in asking!");
}
keyboard.close();
The loop tests its condition at the end of each iteration, after each of the user’s responses.
That’s why the program has a do loop (also known as a do … while loop).
do … while
With a do loop, the program jumps right in, executes some statements, and then checks a condition. If the condition is true, the program goes back to the top of the loop for another go-around. If the condition is false, the computer leaves the loop (and jumps to whatever code comes immediately after the loop).
do
do {
<em> Statements</em>
} while (<em>Condition</em>)
Writing the Condition at the end of the loop reminds the programmer that the computer executes the Statements inside the loop first. After the computer executes the Statements, the computer goes on to check the Condition. If the Condition is true, the computer goes back for another iteration of the Statements.
Condition
Statements
With a do loop, the computer always executes the statements inside the loop at least once:
<strong>/This code prints something:</strong>
int twoPlusTwo = 2 + 2;
System.out.println("Are you kidding?");
System.out.println("2+2 doesn’t equal 5.");
System.out.print ("Everyone knows that");
System.out.println(" 2+2 equals 3.");
} while (twoPlusTwo == 5);
This code displays Are you kidding? 2+2 doesn't equal 5 …and so on and then tests the condition twoPlusTwo == 5. Because twoPlusTwo == 5 is false, the computer doesn’t go back for another iteration. Instead, the computer jumps to whatever code comes immediately after the loop.
Are you kidding? 2+2 doesn't equal 5 …
twoPlusTwo == 5
n
Here's a sample run of the program:
Enter a number: 5
5
Continue? (y/n) y
Enter a number: 81
81
Enter a number: 29
29
Continue? (y/n) n
Done!
int
Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.