0
    int arrayExample = {10, 20, 30, 40, 50};
    for (int i = 0; i < arrayExample.length; i++)
        arrayExample[i]++;
    System.out.println(Arrays.toString(arrayExample));

    arrayExample = new int[]{10, 20, 30, 40, 50};
    for (int i = 0; i < arrayExample.length; i++)
        arrayExample[i++]++;
    System.out.println(Arrays.toString(arrayExample));

produces the following output

[11, 21, 31, 41, 51]

[11, 20, 31, 40, 51]

Why does the second looop increment the elements at index 0, 2 and 4 but not index 1 and 3? Shouldnt it be the opposite (incrementing index 1 and 3 but ignoring the rest?)

Aleksandar
  • 71
  • 7
  • 2
    Why did you write `arrayExample[i++]++` instead of `arrayExample[i]++`? The increment of `i` is already part of the `for` loop – UnholySheep Sep 12 '21 at 21:43
  • 1
    `i++` returns the original value of `i` before incrementing. `++i` returns the value of `i` after incrementing. Looks like you wanted to use `++i` instead of `i++` in the second for loop. – EvilTak Sep 12 '21 at 21:44
  • I understand arrayExample[i]++ is working as intended (increasing the element at index i), but why does arrayExample[i++]++ produce the given output of incrementing only index 0, 2 and 4? – Aleksandar Sep 12 '21 at 21:46
  • Because it increments `i` once more - leading to `i` being incremented twice each iteration, so it skips every other index – UnholySheep Sep 12 '21 at 21:47
  • @EvilTak Exactly but why does the second loop produce the given output (missing incrementation of indices 1 and 3? – Aleksandar Sep 12 '21 at 21:52

4 Answers4

0

If you want to increment every second element in the array, you should change how the iteration loop works:

     for (int i = 1; i < arrayExample.length; i=i+2)
      arrayExample[i]++;

I made three changes, which I will explain:

  1. The loop starts at 1 instead of 0, which is the first element you want to increment (and the second element overall)
  2. The loop goes up by 2 (i=i+2) because you want to increment every second element
  3. Now that every i points to the index of the value you want to change, you always change that value. (Removed an i++)

Apologies for any spelling mistakes, I'm typing on my phone at the moment.

Dharman
  • 30,962
  • 25
  • 85
  • 135
JD9999
  • 394
  • 1
  • 7
  • 18
0

I think you're having trouble conceptualizing the meaning behind i++

What this expression does is return the value of i , and then increment it.

For example:

class Main {
  public static void main(String[] args) {
    int[] nums = {1,2,3,4};
    int i = 0;
    nums[i++]++;
    System.out.println(nums[0] + " " + nums[1]);
    System.out.println(i);
  }
}

This will return:

2 2
1

The value of the 0th index is incremented by 1, and then the value of i is also incremented at the end.

This is why the 0th, 2nd, and 4th values are incremented since you are incrementing i after you return it.

Aniketh Malyala
  • 2,650
  • 1
  • 5
  • 14
0

The array index will be evaluated before i gets incremented by ++. If you want it to be incremented before, use ++i

E.g

int[] arrayExample = new int[]{10, 20, 30, 40, 50};
    for (int i = 0; i +1< arrayExample.length; i++)
        arrayExample[++i]++;
    System.out.println(Arrays.toString(arrayExample));

Will output your expected output [10, 21, 30, 41, 50]

See this answer https://stackoverflow.com/a/32989553/2970422 for the order of evaluation

Joker
  • 2,304
  • 25
  • 36
0

As part of the for loop construct, i is not incremented until the end of the loop.

  • but i is always incremented after the array element is accessed. This is independent of and in addition to i being incremented as part of the loop.
  • then it is incremented again at the end of the loop as part of the loop construct.
arrayExample = new int[]{10, 20, 30, 40, 50};
for (int i = 0; i < arrayExample.length; i++)
        arrayExample[i++]++;
System.out.println(Arrays.toString(arrayExample));

So what you are essentially doing is

arrayExample = new int[]{10, 20, 30, 40, 50};
for (int i = 0; i < arrayExample.length; i += 2)
        arrayExample[i]++;
System.out.println(Arrays.toString(arrayExample));

So i will take on the values 0, 2, and 4 and then the loop terminates. Both of the above print

[11, 20, 31, 40, 51]
WJS
  • 36,363
  • 4
  • 24
  • 39