简体   繁体   中英

The iteration variable is +1 outside the for-loop?

I have an array of size 4 and I want to check if the array contains the number 8 (which it obviously does not, the code is just for testing).

In the for-loop j goes from 0 to 3, so the final value of j in the loop is 3. However I don't follow why the value of j after the loop has been changed to 4, why is it still not 3?

public class Test {
    public static void main (String[] args) {
        int[] a = new int[4];
        a[0] = 2; 
        a[1] = 3;
        a[2] = 4;
        a[3] = 5;
        int n = a.length;    // n = 4
        int number = 8;
        int j;
        for (j = 0; j < n; j++) {
            if (a[j] == number) {
                System.out.println("The number is at place " + j);
                break;
            }
            // Last value of j is 3:
            System.out.println("Value of j after each iteration " + j); 
        }
        // But here j is 4?
        System.out.println("Value of j after the for-loop: " + j); 
     }
}

The output:

Value of j after each iteration 0
Value of j after each iteration 1
Value of j after each iteration 2
Value of j after each iteration 3
Value of j after the for-loop: 4

Yes because at the end of a for loop there is an increment to the variable. A for loop can be rewritten as:

int j = 0;
while(j < n) {
    //code
    j++;
}

So on the last iteration j will be incremented, it will go to the condition, and it will be false so the body of the for loop will not be entered. In order for the loop to end, j has to be more than or equal to the condition.

New to programming?

for(initialization; booleanExpression; updateStatement) {
  ; // Body
}

The steps is,

  1. Initialization statement executes
  2. If booleanExpression is true continue, else exit loop
  3. Body executes
  4. Execute updateStatements
  5. Return to Step 2

So the end value should be 4

Think about it...

This is your for loop:

 for (j = 0; j < n; j++){
   //your code here
 }

You start your for loop with j = 0 and every time you iterate through it, you have to check if the value is less than n ( j < n ). To achieve that you have to increment it on every iteration.

So this is what happens for n = 4:

  • 1st iteration:

    j = 0; 0 < 4 == true; // you execute your code j++; //As you can see you increment before you continue to the next iteration

  • 2nd iteration:

    j = 1; // j now equals 1 because you incremented it on the previous iteration 1 < 4 == true; // you execute your code j++;

  • 3rd iteration:

    j = 2; 2 < 4 == true; // you execute your code j++;

  • 4th iteration:

    j = 3; 3 < 4 == true; // you execute your code j++;

  • 5th iteration:

    j = 4; 4 < 4 == false; // loop ends

As you can see, when your code is about to start the fifth iteration, the j variable now equals 4 so it doesn't pass the j < n criteria.

However, it still incremented in the 4th iteration so you get j = 4.

This was how my teacher explained it to me when I was just starting my coding education, I hope it helps you as it helped me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM