Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

DoWhile Loop

Problem : Display "Hello Java" five times on the console with help of do-while statements

Program:

Step 1 - Coding

Create a text file HelloDoWhile.java  and copy below contents.

/**
 *  A program that prints 'Hello Java' string with help of DoWhile loop statements.
 */

public class HelloDoWhile {

    public static void main(String[] args) {
        int i = 0;
        do {

            System.out.println( i+ " Hello Java ");
            i++;
        } while (i < 5);
    }
}


Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy HelloDoWhile.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile HelloDoWhile.java with help of 
    • javac HelloDoWhile.java command.Command will create class file in the same folder.
  5. Congratulations!! your Control Statement Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your Control Statements java program from command prompt with help of 
    • java HelloDoWhile command.

Output


It will print Hello five times.
0 Hello Java
1 Hello Java
2 Hello Java
3 Hello Java
4 Hello Java

FAQ

What is do-while loop?

The do-while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

do {
statement(s)
} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

<<Previous | Next>>
ċ
HelloDoWhile.java
(0k)
Mahima Hardia,
Aug 19, 2011, 3:08 AM
Comments