I am not sure what this line of code does? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I am not sure what this line of code does?

sum += myArr[x];

19th Mar 2017, 9:53 PM
Stefan
Stefan - avatar
2 Answers
+ 8
sum is a variable (you know that) += adds the second value to the first myArr[x] is the value in the array at x (remember array values start from zero) So sum += myArr[x]; adds the value of myArr at x to sum. The same, it adds the value that is the x+1 th term in the array myArr. Example: int sum = 4, x = 2; myArr[2] = 7; //myArr needs to already be declared sum += myArr[x]; System.out.print(sum); /* Outputs 11 */
19th Mar 2017, 10:05 PM
J.G.
J.G. - avatar
0
this line means the progarm will add all the elements of an array sum is a predefined variable who's objective is to hold the value of the result sum = sum + myArr[x]
19th Mar 2017, 10:07 PM
Abhishek Basak
Abhishek Basak - avatar