×

Search anything:

for loop in Java

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 15 minutes | Coding time: 2 minutes

for loop is an entry controlled loop that is widely used in Java programming language.

Loop is a programming concept which is natively supported by nearly all programming languages. Loops are used to repeat a particular coding task where each repetition follows a particular pattern which can be incorporated in a code. Example of such a task is printing integers from 1 to 10. The repetition is that we are printing integers and the pattern is that we start from 1 and increment by 1 each step. In this article, we will take a look at loops in Java.

The concept of loops comes into picture when we need to implement/print a particular block/statement more than one time till a particluar condition is specified. We have different types of loops :

  1. Entry Controlled loop : Where the entry of the loop is controlled that is we have a condition at the starting of the loop. Example : while loop

  2. Exit Controlled loop : In this type of loop the exit from the loop is controlled that is we have a condition at the end of the loop. Example : do..while loop

topic of image

In this particular section we are going to discuss for loop

for loop Syntax :

for (initializationStatement; testExpression; updateStatement)
{
           // codes 
}

Where :

  • initializationStatement is the statement where we to initialize the loop counter to some value. Example : int i=0;

  • testExpression in this expression we have to test the condition. If the condition is to true then we will execute the body of loop otherwise we will exit from the for loop. Example : i<=10;

  • updateStatement after executing loop body this statement is encountered and is incremented/decremented depending on the statement. Example : i++

Order of the excution of for loop

  1. First we intialize the loop counter in Java one have to declare the variable before using it or it can be declared as well as intialized in the intializationStatement as well.

  2. Then the condition is checked i.e testExpression is evaluated.

  3. After this we enter the loop and print/execute the statement(s)/block.

  4. Then the value of the loop counter in incremented/decremented depending on the updateStatement.

  5. Now we will repeat Step 2-4 till the testExpression condition is violated.

topic of image

Example

In this example, we will write a program to print "OpenGenus" five times with number from 1 to 5 with it.

Answer :

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

Points to remember

  1. It is not compulsory to specify initializationStatement or testExpression or UpdateStatement during declaration of for loop.

Example:

class for_loop 
{
	public static void main (String[] args) 
	{
        int i=2;
        // Without Initialization Statement
		for(; i<=5; i++)
		{
		    System.out.println("OpenGenus "+i);
		}
        // Without Test expression
		for(i=1; ;i++)
		{
		    System.out.println("OpenGenus "+i);
            if(i==5)
                break;
		}
        // Without Update Statement
		for(i=4; i<=5;)
		{
		    System.out.println("OpenGenus "+i);
            i++;
		}
	}
}
  1. You can declare updatStatement inside the block also.
    Example :
class for_loop 
{
	public static void main (String[] args) 
	{
        int i=2;
        // With Update Statement inside block
		for(i=4; i<=5;)
		{
		    System.out.println("OpenGenus "+i);
            i++;
		}
	}
}
  1. One can include more than one initializationStatement or testExpression or updateStatement during declaration of for loop this can be done with the help of logical operations such as AND(&&) / OR(||).
class for_loop 
{
	public static void main (String[] args) 
	{
		for(int i=4, j=2; i<=6 && j>=0; i++, j--)
		{
		    System.out.println("OpenGenus "+i +" "+ j);
		}
	}
}
  1. You should always declare the loop with small case i.e for and not FOR/For/FoR...etc as Java is a case sensitive langauage and this true for all other loops as well

  2. Infinite loop

To create any inifinite loop, you need to ensure that the test condition never evaluates to false.

Example:

class for_loop 
{
	public static void main (String[] args) 
	{
        // Infinite loop
		for(;;)
		{
		}
	}
}

Question

What is this for(;;); applicable for?

Infinite loop
Invalid loop
0 loop
1 loop
This statement is applicable and is known as infinite loop i.e the loop will not have any terminating condition and will continue running till we exit the program.
OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Improved & Reviewed by:


for loop in Java
Share this