Logout

The For Loop

The for loop is the most commonly kind of loop, not only in Java, but in pretty well every programming language that exists today. One of the main reasons it is very commonly used is because it enables efficient traversal through arrays, rather than copying and pasting for every element in the array. But, beyond that, it is also used whenever there is a specific number of times a loop is to be executed.

So, whereas the number of times a while loop will loop is unknown when it is first entered, the number of times a for loop will loop is known for certain, right from the time it is first entered.

Like the while loop, there is a loop control variable. And like the while loop:

But the big difference, compared to the while loop, is that with the for loop, all three of these loop control variable statements are found together within one pair of parenthesis that immediately follow the for keyword. As follows:

for(int i = 0; i < 10; i++){
    stuff to be done ten times
}

This is all very unorthodox compared to all other Java statements, but because practically all other programming languages format a for loop this way, so does Java.

You can understand what follows the for as three separate lines, combined into one. The first part is an initinalization of the loop control variable, the second is the condition which if true results in continuation of the loop, and the third part is the part that pushes the loop control variable toward a bound that will cause the condition to be false.

You read the above for statement like this: "For a variable called i that starts at 0, as long as i is less than 10, do what follows in the loop block, and at the end, each time, increase the value of i by one.

 

Examples:

 1 /*

10 public class ForLoops {
11     public static void main(String[] args) {
12 
13         for(int i = 0; i < 10; i++){//Normal for loop to loop 10 times.
14             System.out.println(i);
15         }
16 
17         for(int i = 10; i > 0; i--){//A for loop to loop 10 times, but doing so the reverse way.
18             System.out.println(i);
19         }
20 
21         for(int i = 0; i < 10; i+=2){//A for loop which will loop 5 times.
22             System.out.println(i);
23         }
24 
25         for(int i = 10; i > 0; i-=3){//A loop which will loop 4 times, in a reverse way.
26             System.out.println(i);
27         }
28         
29         for(int i = 0; i > 10; i++){// A for loop which will never be entered
30             System.out.println(i);
31         }
32 
33         for(int i = 0; i < 10; i--){//A for loop which will cause an invinite loop!!
34             System.out.println(i);  //********Will cause the computer to freeze*********
35         }
36         
37         
38         //An actual example of using a for loop; in this case for assigning, and then printing out an array.
39         
40         int [] myIntArray = new int[10];
41         
42         for(int i = 0; i < myIntArray.length; i++){
43             myIntArray[i] = i*100;
44         }
45         
46         for(int i = 0; i < myIntArray.length; i++){
47             System.out.println(myIntArray[i]);
48         }
49     }
50 }
52 

See below for the output of the above program, but here are a couple of things to note first:

If you make an infinite loop by accident - as is in lines 33 to 35 above - and run it in Netbeans, the way you end the process is by clicking on the little x by the progress bar in the bottom right hand corner of the Output window.

When you are looping through arrays, for the condition that allows the loop to continue, you should use the .length attribute that all arrays have, rather than using the integer number which you (think you) know is the length of the array. Because the .length attribute will always be right, and you may make a mistake when typing in the number you think is the length.

0
1
2
3
4
5
6
7
8
9

10
9
8
7
6
5
4
3
2
1

0
2
4
6
8

10
7
4
1

The fifth loop above is never entered.

The sixth loop above would cause an infinite loop, so I'll take a pass on attempting the output :)

0
100
200
300
400
500
600
700
800
900