For Loops!

Learn how to manipulate for loops in java

What are they for?

In programming, the for statement is known as an iterator, which is a fancy way of saying something that repeats a procedure. It is commonly referred to as a 'for loop' because it does just that, it loops. The for loop creates a space in a program where code written within the curly braces in the body of the statement will repeat until a certain condition is met. The condition to terminate the for loop is up to the programmer. The most common form of a java for loop looks like this:

for( initialization; condition; increment/decrement) {

    // body of the code

}

Firstly, we initialize a variable that can be declared in the same space or before the for loop

for(int count  =  0; condition; increment/decrement) {

}

or

int count;

for(count  =  0;  condition; increment/decrement) {

}

Secondly, we provide the statement with a condition that, once met, will terminate the for loop so that the code following the loop can be executed.


int number  =  5;

for(int count  =  0; count  <  number; increment/decrement) {

}

Finally, we allow the variable count to be incremented with every run-through of the for loop so that once it is no longer less than the variable number the loop will end.

class ForStatement {

    public static void main(String[]  args){

        int number  =  5;

        for(int count  =  0; count  <  number; count++) {

            System.out.println("The number is "  + count);    

        }
    }   

}

count++ adds one on to the variable count every time the loop reaches the end of its body without being terminated.

So the output for this code is:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

The for loop runs through once before being incremented or decremented so the first output will be the value given to the variable when it is initialized.

There is an alternate format for the for loop commonly used with arrays, known as the enhanced for loop. The format for this enhanced for looks like this:

for( dataType variableName  :  arrayVariableName) {

}

Given an array:

int[] anArray = {1, 2, 3, 4, 5};

The enhanced for loop would look like this:

 class EnhancedFor {

    public static void main(String[] args) {

        int[] anArray = {1, 2, 3, 4, 5};

        for( int count : anArray) {

            System.out.println(“The number is “ + count);

        }
    }
}

The output for this code results as follows:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

It is also possible to have what is called a nested for loop. This is when a for loop is contained within another for loop.

E.g.

class NestedFor {

    public static void main(String[] args){

        int number = 1;

        int count;

        for( count = 1; count < 4; count++) {

            for( number = 1; number < 5; number++) {

               System.out.println(count + " plus " + number +
               " is " + (count + number));

             }

             System.out.println(""); //leaves a space between blocks of output statements

        }
    }
 }

This code output is:

1 plus 1 is 2
1 plus 2 is 3
1 plus 3 is 4
1 plus 4 is 5

2 plus 1 is 3
2 plus 2 is 4
2 plus 3 is 5
2 plus 4 is 6

3 plus 1 is 4
3 plus 2 is 5
3 plus 3 is 6
3 plus 4 is 7

Deciding which iterator to use is left to the discretion of the programmer.

Comments

blog comments powered by Disqus