+ 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);

9th Jun 2017, 8:19 PM
D_Stark
D_Stark - avatar
6 Respostas
+ 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
9th Jun 2017, 8:39 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 12
I explained the same code in this thread, have a look : https://www.sololearn.com/discuss/400109/?ref=app
9th Jun 2017, 8:24 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
Wow did I really ask this question 2 years ago lol
30th Sep 2019, 9:12 PM
D_Stark
D_Stark - avatar
+ 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++"
9th Jun 2017, 8:22 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
thanks guys im new to java and this app i didnt realise that i could search posts great stuff.
9th Jun 2017, 8:34 PM
D_Stark
D_Stark - avatar
0
it means x is incremented by 1 each time until the loop runs..
9th Jun 2017, 8:35 PM
RASHMI SHARMA
RASHMI SHARMA - avatar