Trail: Learning the Java Language The while and do-while Statements statement to continually execute a block of statements while a condition remains true. Loop is used in programming to repeat a specific block of code. In this article, you will learn to create while and do while loops in Java programming. The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s).


Author: Jaden Schumm
Country: Viet Nam
Language: English
Genre: Education
Published: 22 July 2015
Pages: 627
PDF File Size: 17.12 Mb
ePub File Size: 4.17 Mb
ISBN: 666-9-39055-371-5
Downloads: 3935
Price: Free
Uploader: Jaden Schumm


Java's While and Do-While Loops in Five Minutes — SitePoint

Thus, the statements within the block associated with a do-while are executed at least once. Here's the previous program rewritten to use do-while shown in boldface and renamed to DoWhileDemo: Based on the result of the evaluation, the loop either terminates or a new iteration is started.

This is the standard input stream which in most cases corresponds to keyboard input. In a guessing game we would like to prompt the player for an answer do while java least once and do it until the player guesses the correct answer. A do-while loop fits perfectly do while java.


In the loop body we receive input from the player and then the loop condition checks whether it is the do while java answer or not. The loop must run as long do while java the guess does not equal Daffy Duck. If a correct answer is received, the loop terminates and we congratulate the player.

Javanotes , Section -- The while and Statements

One other issue is addressed by the program: If the user enters zero as the first input value, there do while java no data to process.

We can test for this case by checking whether count is still equal to zero after the while loop.


This might seem like a minor point, but a careful programmer should cover all the bases. Here is the program and an do while java that simulates it: The word "do" is added to mark the beginning of the loop. This semicolon is part of the statement, just as the semicolon at the end of an assignment statement or declaration is part of the statement.

Omitting it is a syntax error. To execute a do loop, do while java computer first executes the body of the loop -- that is, the statement or statements inside the loop -- do while java then it evaluates the boolean expression. If the value of the expression is true, the computer returns to the beginning of the do loop and repeats the process; if the value is false, it ends the loop and continues with the next part of the program.

  • Java while and dowhile Loop
  • Do while loop in java
  • The while and do-while Statements
  • While Loop

Since the condition is not tested until the end of the loop, the body of a do loop is always executed at least once. For example, consider the following pseudocode for a game-playing program. The do loop makes sense here instead of a while loop because with the do loop, you know there will be at least one game.

Also, the test that is used at the end of the loop wouldn't even make sense at the beginning: The while statement continues testing the expression and executing its block until the expression evaluates to false.

Java while Loop The syntax of while do while java is: The test expression inside parenthesis is a boolean expression. If the test expression is evaluated to true, statements inside the do while java loop are executed. This process goes on until the test expression is evaluated to false.