C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

what is the purpose of for staement in java?

0 votes
71 views
asked May 8, 2014 by rock Expert (14,990 points)

1 Answer

0 votes
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
  }

}
answered May 8, 2014 by sunny Expert (11,410 points)

Related questions

0 votes
2 answers 33 views
0 votes
1 answer 17 views
17 views asked Dec 21, 2013 by rajesh Guru (39,140 points)
0 votes
1 answer 30 views
30 views asked Feb 7, 2014 by john Guru (27,140 points)
0 votes
1 answer 5 views
5 views asked Jan 4, 2014 by smith Geek (15,340 points)
0 votes
1 answer 11 views
11 views asked Dec 17, 2013 by john Guru (27,140 points)
...