dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Dummies AI
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
forward arrow
  • Custom Solutions
  • Dummies AI
    • Log In
    Explore Book
    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.

    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: 

    Beginning Programming with Java For Dummies

    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: 

    Java
    Trending
    Moving Abroad For Dummies Cheat Sheet
    Trig Identities for Pre-Calculus
    Captain America For Dummies Cheat Sheet
    What Are Somatic Exercises and Their Benefits?
    How to Make a Margarita
    From Category
    Java
    Important Features of the Java Language
    How to Download and Install TextPad
    How to Install JavaFX and Scene Builder
    Getting a Value from a Method in Java
    A Few Things about Java GUIs

    Quick Links

    About For Dummies
     Terms of Use
    Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
    © 2025 MARVEL

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant