Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Loops

Java Programming Language Loop

In computer programming, looping is the process of executing the same thing again & again.

In Java programming, loops are used when there is a need to execute some specific part of the program repeatedly.

Assume that there is a need to print our individual name 20 times repeatedly then it is very troublesome to write it 20 times in a program. The only way to avoid this type of programming approach is to use loops. Using loops will help us to write name only 1 time and printing it 20 times.

There are three types of loops in Java programming:
i) for loop 
ii) while loop
iii) do-while loop

The for loop

Syntax

for(initialization;condition;update)
{
    // programming
    // statements
}

The for loop consists of three expressions called initialization, condition & update and its body (statements inside curly braces).

Flowchart

Java Programming Language for loop Flowchart

Working

Java Programming Language for loop Working

Step I –
             Program control executes initialization expression.

Step II –
            After initialization, the condition is checked.

Step III –
            If the condition is true, the body of for loop is executed.

Step IV –
            After execution of for loop body, program control jumps to update expression.

Step V –
          After execution of update expression program control again returns to condition expression to check whether the condition becomes true or false.

If the condition is true again Step-III, Step IV & Step V are repeated.

This process goes on till condition does not become false.

Example No. 1

class ForLoopDemo1 
{
    public static void main(String args[]) 
    {
        int i;
        for(i=1;i<=10;i++)
        {
            System.out.println("John");
        }
    }
}

Output

John
John
John
John
John
John
John
John
John
John

Thus with the help of for loop in the above program, we have succeeded to print name 10 times by writing it just 1 time.

Example No. 2

class ForLoopDemo2 
{
    public static void main(String args[]) 
    {
        int n;
        for(n=1;n<=10;n++)
        {
            System.out.print(n+" ");
        }
    }
}

Output

1 2 3 4 5 6 7 8 9 10

Here we have succeeded to print numbers 1 to 10 by writing just single line

System.out.print(n+" ");

in for loop.

Is it right?

Case – I

for(     ;condition;update)
{
    // programming
    // statements
}

Yes of course! We can avoid writing expressions in the for loop.

Example

class ForLoopDemo3 
{
    public static void main(String args[]) 
    {
        int n;
        n = 1; // Initialization
        for( ;n<=10;n++)
        {
            System.out.print(n+" ");
        }
    }
}

Output

1 2 3 4 5 6 7 8 9 10

Case – 2

for(initialization;      ;update)
{
    // programming
    // statements
}

Example

class ForLoopDemo4 
{
    public static void main(String args[]) 
    {
        int n;
        for(n=1; ;n++)
        {
            if(n<=10)   //condition
            {
                System.out.print(n+" ");
            }
            else
            {
                break;
            }
        }
    }
}

Output

1 2 3 4 5 6 7 8 9 10

Case – 3

for(initialization;condition;    )
{
    // programming
    // statements
}

Example

class ForLoopDemo5 
{
    public static void main(String args[]) 
    {
        int n;
        for(n=1;n<=10;)
        {
            System.out.print(n+" ");
            n++; // update
        }
    }
}

Output

1 2 3 4 5 6 7 8 9 10

Case – 4

for(    ;    ;    )
{
    // programming
    // statements
}

Example

class ForLoopDemo6 
{
    public static void main(String args[]) 
    {
        int n;
        n = 1; // initialization
        for( ; ; )
        {
            if(n<=10) // condition
            {
                System.out.print(n+" ");
            }
            else
            {
                break;
            }
            n++; // update
        }
    }
}

Output

1 2 3 4 5 6 7 8 9 10

 

Leave a reply