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

While Loop

Problem: Write a program to print 'Hello Java' on console with help of while loop


Step 1 - Coding

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

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

public class HelloWhile {

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

            System.out.println("Hello Java ");

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy HelloWhile.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile HelloWhile.java  with help of 
    • javac HelloWhile.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 HelloWhile command.


It will print Hello five times.
Hello Java
Hello Java
Hello Java
Hello Java
Hello Java


What is while loop?

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
while (expression) {
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

<<Previous | Next>>
Mahima Hardia,
Aug 19, 2011, 3:07 AM