Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
To write the Java program you see below, you need a loop — a loop that repeatedly asks the user whether the importantData.txt file should be deleted. The loop continues to ask until the user gives a meaningful response.

/*

* 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:_______________________________ */

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> </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> </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.

java loop using java do statement
Here we go loop, do loop.

That’s why the program has a do loop (also known as a do … while loop).

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).

A closer look at the do statement

The format of a do loop is

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.

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;

do {

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.

Do I hear an echo?

In a do statement, repeatedly read numbers from the keyboard. Display each number back to the user on the screen. After displaying a number, ask whether the user wants to continue entering numbers. When the user replies with the letter n, stop.

Here's a sample run of the program:

Enter a number: 5

5

Continue? (y/n) y

Enter a number: 81

81

Continue? (y/n) y

Enter a number: 29

29

Continue? (y/n) n

Done!

Tally ho!

In a do statement, repeatedly read int values from the keyboard and keep track of the running total. The user says, “I want to stop entering values” by typing one final int value — the value 0. At that point, the program displays the total of all values that the user entered.

About This Article

This article is from the book:

About the book author:

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.

This article can be found in the category: