Monday, November 11, 2013

Flow Controls - for Loop

General form of the traditional for statement :
for( initialization; condition; iteration ){
               // body
}
if only one statements is being repeated, there is no need for the curly braces.
The for loop operates as follows. when the loop first starts, the initialization portion of the loop is executed.Generally , this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. it is important to understand that the initialization expression is only executed once, Next condition is evaluated, this must be a boolean expression, it usually tests loop control variable against a target value, if this expression is true , then the body of the loop is executed, if it is false, the loop terminates. next the iteration portion of the loop is executed. this is usually an expression that increments or decrements the loop control variable, the loop then iterates, first evaluation the conditional expression, then executing the body of the loop , and then executing the iteration expression with each pass, this process repeats until the controlling expression is false.
here is a version of the "tick " program that uses a for loop.

Declaring Loop Control variables inside the for loop

class ForLoopTest {

    public static void main(String[] args) throws IOException {
     
        int n;
        for(n=10; n>0; n--){
            System.out.println("tick "+n);
        }
    }
}
Often the variable that controls a for loop is only needed for the purpose of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the for. For example, here is the preceding program re-coded so that the loop control variable n is declared as an int inside the for:
when you declare a variable inside a for loop, there is one important point to remember, the scope of the
class ForLoopTest {

    public static void main(String[] args) throws IOException {
     
        for(int n=10; n>0; n--){
            System.out.println("tick "+n);
        }
    }
}
variable ends when the for statement does, (that is, the scope of the variable is limited to the for loop) Outside the for loop, the variable will cease to exist, if you need to use the loop control variable elsewhere in your program, you will not be able to do it inside the for loop.
when the loop control variable will not be needed elsewhere, most java programmers declare it inside the for.EX : the following program tests for prime numbers Notice that the loop control variable, i is declared inside the for since it is not needed elsewhere,
class ForLoopTest {

    public static void main(String[] args) throws IOException {

        int num;
        boolean isPrime = true;
        num = 14;
        for (int i = 2; i < num / i; i++) {
            if ((num % i) == 0) {
                isPrime = false;
                break;
            }

            if (isPrime) {
                System.out.println("Prime");
            } else {
                System.out.println("Not Prime");
            }
        }
    }
}
The for loop supports a number of variation that increases its power and applicability. The reason it is so flexible is that its three parts, the initialization, the conditional test, and the iteration, do not need to be used for only those purposes, In fact, the three sections of the for can be used for any purpose you desire, Let's look at some examples.
One of the most common variations involves the conditional expression, Specifically , this expression does not need to test the loop control variable against some target value. In fact, the condition controlling the for can be any boolean expression.

class ForLoopTest {

    public static void main(String[] args) throws IOException {

       boolean done = false;
       for(int i = 10; done; i++){
           if(i==5) done =false;
       }
     
    }
}
Here, the for loop continues to run until the boolean variable done is set true, it does not test the value of i.
Here is another interesting for loop variation, either the initialization or the iteration expression or both may be absent, as in this next program.
class ForLoopTest {

    public static void main(String[] args) throws IOException {

        int i;
        boolean done = false;
        i = 0;
        for (; !done;) {
            System.out.println("i is :" + i);
            if (i == 10) {
                done = true;
            }
            i++;
        }
    }
}
Here, the initialization and iteration expressions have been moved out of the for, thus, parts of the for are empty while this is of no value in this simple example indeed, it would be considered quite poor style there can be times when this type of approach makes sense ,
Eg :- if the initial condition is set through a complex expression elsewhere in the program or if the loop control variable changes in a non sequential manner determined by actions that occur within the body of the loop, it may be appropriate to leave these the for empty.
Here us one more for loop variation, You can intentionally create an infinite loop (a loop that never terminates ) if you leave there parts if the for empty.
Eg:
for( ; ; ){
          // body
}

Nested loops 

like all other programming languages, Java allows loops to be nested, that is one loop may be inside another

class ForLoopTest {

    public static void main(String[] args) throws IOException {

        int i,j;
        for(i=0; i<10; i++){
            for (j=i; j<10; j++){
                System.out.print(".");
            }
             System.out.println("");
        }
    }
}
Output
..........
.........
........
.......
......
.....
....
...
..

.


No comments:

Post a Comment