Developer community
0 votes
81 views
asked by Expert (15k points) 15 23 29 | 81 views

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 by Expert (11.4k points) 14 22 29

Related questions

0 votes
2 answers
asked by john Guru (27.1k points) 31 48 96 | 37 views
0 votes
1 answer
asked by rajesh Guru (39.1k points) 10 22 34 | 19 views
0 votes
1 answer
asked by john Guru (27.1k points) 31 48 96 | 30 views
0 votes
1 answer
asked by smith Geek (15.3k points) 15 40 60 | 6 views
0 votes
1 answer
asked by john Guru (27.1k points) 31 48 96 | 11 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users