4

I am newbie but cannot find the answer to this question after looking thoroughly in other questions.

Why is there a need sometimes to declare a variable and sometimes not? I give you two examples:

Example 1: here, we don't have to declare i before the for loop.

class ForDemo {
    public static void main(String[] args){
         for(int i=1; i<11; i++){
              System.out.println("Count is: " + i);
         }
    }

Example 2: Here we need to declare i before the loop.

class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] arrayOfInts = { 
            { 32, 87, 3, 589 },
            { 12, 1076, 2000, 8 },
            { 622, 127, 77, 955 }
        };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;

    search:
        for (i = 0; i < arrayOfInts.length; i++) {
            for (j = 0; j < arrayOfInts[i].length;
                 j++) {
                if (arrayOfInts[i][j] == searchfor) {
                    foundIt = true;
                    break search;
                }
            }
        }

        if (foundIt) {
            System.out.println("Found " + searchfor + " at " + i + ", " + j);
        } else {
            System.out.println(searchfor + " not in the array");
        }
    }

I will appreciate if you could also tell me why do I have also to initialize the variable j = 0 if within the for loop I am already assigning the value 0.

7
  • 1
    Rule of thumb: Begin with the most narrow block. And only when you need, move it "up". Commented Nov 13, 2017 at 9:13
  • Because you use it after the closing } of the for loop. The scope ends there so you have to declare the variable so it can be accessed afterwards. Maybe you should read a bit about scopes. Commented Nov 13, 2017 at 9:13
  • The variable doesn't live after its scope ends. Commented Nov 13, 2017 at 9:14
  • because you are accessing i variable after for loop execution. variables defines in for have scope only till loop execution. Commented Nov 13, 2017 at 9:14
  • as for j, there is no guarantee your loops will run, so if you would try to print it after the loops, it would need to be set Commented Nov 13, 2017 at 9:16

2 Answers 2

2

If you declare the loop variable within the loop, it is only accessible inside the loop.

Since, in your last snippet, you are accessing both i and j after the loop ends (in System.out.println("Found " + searchfor + " at " + i + ", " + j);), you must declare them before the loop.

That's also the reason why you have to initialize j prior to the loop. The loop may never be executed (if arrayOfInts.length is 0), but you would still access j in the mentioned println statement, so it must be initialized at this point.

Sign up to request clarification or add additional context in comments.

Comments

2

In Example 1, i is only used within the for-loop. That's why you do not need to declare it beforehand. In Example 2, i is used in the loop, but also afterwards in the "if (foundIt)"-Block. Therefore, it must be declared outside the loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.