Wednesday, November 2, 2011

For Loop

 The for Statement



The for statement is like the while statement, i.e. you use it to create loop.
The for statement has following syntax:

for init ; booleanExpression ; update ) {
    statement (s)
}

  1. init is an initialization that will be performed before the first iteration.
  2. booleanExpression is a boolean expression which will cause the execution of statement(s)if it evaluates to true.
  3. update is a statement that will be executed after the execution of the statement block.
  4. init, expression, and update are optional.
The for statement will stop only if one of the following conditions is met:
  1. booleanExpression evaluates to false
  2. A break or continue statement is executed
  3. A runtime error occurs.



 For statement in detail



It is common to declare a variable and assign a value to it in the initialization part.
 The variable declared will be visible to the expression and update parts as well as to
 the statement block.For example, the following for statement loops five times and
 each time prints the value of i. Note that the variable i is not visible
 anywhere else since it is declared within the for loop.
public class MainClass {

  public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
        System.out.println(i + " ");
    }
  }
}
The initialization part of the for statement is optional.
public class MainClass {

  public static void main(String[] args) {
    int j = 0;
    for (; j < 3; j++) {
      System.out.println(j);
    }
    // j is visible here
  }

}
The update statement is optional.
public class MainClass {

  public static void main(String[] args) {
    int k = 0;
    for (; k < 3;) {
      System.out.println(k);
      k++;
    }
  }

}
You can even omit the booleanExpression part.
public class MainClass {

  public static void main(String[] args) {
    int m = 0;
    for (;;) {
      System.out.println(m);
      m++;
      if (m > 4) {
        break;
      }
    }
  }

}
If you compare for and while, you'll see that you can always replace
 the while statement with for. This is to say that
while (expression) {
    ...
}

can always be written as

for ; expression; ) {
    ...
}





A loop allows you to execute a statement or block of statements repeatedly



public class MainClass {
  public static void main(String[] args) {
    for (int i = 0; i < 8; i++) {
      System.out.println("Hi.");
    }
  }
}
Hi.
Hi.
Hi.
Hi.
Hi.
Hi.
Hi.
Hi.

The numerical for loop



for (initialization_expression ; loop_condition ; increment_expression) {
  // statements
}

public class MainClass {
  public static void main(String[] args) {
    int limit = 20// Sum from 1 to this value
    int sum = 0;    // Accumulate sum in this variable

    for (int i = 1; i <= limit; i++) {
      sum = sum + i;
    }
    System.out.println("sum = " + sum);
  }
}
sum = 210




Infinite For loop Example


public class Main {

  public static void main(String[] args) {
    for (;;) {
      System.out.println("Hello");
      break;
    }
  }
}
//Hello







http://www.java2s.com/Tutorial/Java/0080__Statement-Control/
InfiniteForloopExample.htm




No comments:

Post a Comment