Java Loops

This tutorial explains how Java loops are created and how did they work.

Very often you will want to execute code fragments in your programs several times until some conditions are met. This is what loops are made for.

Java “for” Loop

With the help of the for loop you can execute a sequence of code lines multiple times. The “for” loop in Java works exactly as in other programming languages like C/C++. The for loop is very powerful and yet simple to learn. It is mostly used in searching or sorting algorithms as well in all cases where you want to iterate over collections of data. Here is a simple example of a “for” loop:

public class ForLoopExample {
	public static void main(String[] args) {
		for(int i=0; i<5; i++) {
			System.out.println("Iteration # " + i);
		}
	}
}

The output of the example is as follows:

Iteration # 0
Iteration # 1
Iteration # 2
Iteration # 3
Iteration # 4




The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
    statement(s)
}

The initialization part is where you declare your loop variables. Please note the variables initialized here will be only visible in the loop and destroyed after the loop is finished. In our example we initialize a new int variable named i and assign the value of zero to it.

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

Termination is the boolean statement, which tells the loop for how long to be executed. Until the termination statement is true the loop will continue. In our example we are checking if the value of i is less than 5

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

The third parameter – increment – is executed after each cycle of the loop. Here we can increment the values of the variables declared in the initialization part.

What we do in our example is to increase the value of i by 1 after each loop cycle. i++ is a short form and does exactly the same as i=i+1.

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

Java “while” Loop

Another loop in java is the while loop.

The general form of the while loop can be expressed as follows:

while(condition) {
	// execute code here
}

Condition is boolean. This means until the condition is true the while loop will be executed. I will recreate our first example this time using while loop instead of for loop.

public class WhileLoopExample {
	public static void main(String[] args) {
		int i=0;
		while(i<5) {
			System.out.println("Iteration # " + i);
			i++;
		}
	}
}

Now look at the example above. The initialization of our control variable i is done outside of our loop but the increment by 1 is done inside the loop. The output is exactly the same as in our first example:

Iteration # 0
Iteration # 1
Iteration # 2
Iteration # 3
Iteration # 4

 

Loops are very often used in combination with arrays. In our next tutorial Java Arrays I will explain how to create, use and iterate over arrays.

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply