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

For Loop

Problem : Write a program that prints 'Hello Java' five times on console.

Program :

Step 1 - Coding

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

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

public class HelloFor {

    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {

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

Step 2 - Deployment

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


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


What is for loop?

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:
for (initialization; termination; increment) {
When using this version of the for statement, keep in mind that:
  • The initialization expression initializes the loop; it's executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or\ decrement a value.

<<Previous | Next>>
Namrata Sisodiya,
Aug 19, 2011, 3:06 AM