+ 1
Hi Can anyone break this down and explain it to me please im confused as hell. Whats x++ mean?
int [ ] myArr = {6, 42, 3, 7}; int sum=0; for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } System.out.println(sum);
6 Answers
+ 13
x++ means x=x+1
More precisely,
new value of x = 1 + old value of x
As you can see, the initial value of x is 0 inside for loop, in 2nd iteration x should be 1, in next iteration x should be 2,... and it goes like this.
Suppose you want x to be incremented by 2 in a for loop, you can write the 3rd portion of for loop as follows:
for(int x=0; x<arr.length; x=x+2){
// implementation
}
Now, the values of x should be : 0, 2
+ 12
I explained the same code in this thread, have a look :
https://www.sololearn.com/discuss/400109/?ref=app
+ 6
Wow did I really ask this question 2 years ago lol
+ 2
oh come on. It is covered in tutorial. And there's search function - it's like most common question out here, along with "whats the difference between ++x and x++"
+ 1
thanks guys im new to java and this app i didnt realise that i could search posts great stuff.
0
it means x is incremented by 1 each time until the loop runs..