public class Program { public static void main(String[] args) { int [ ] myArr = {6, 42, 3, 7}; int sum=0; for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } System.out.println(sum); } } very lovely program. please who can explain the meaning of "+=" in the code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

public class Program { public static void main(String[] args) { int [ ] myArr = {6, 42, 3, 7}; int sum=0; for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } System.out.println(sum); } } very lovely program. please who can explain the meaning of "+=" in the code?

why do we have to use += rather than just + symbol to show the addition of sum to the output of myArr [x]?

20th Oct 2016, 3:24 PM
Raymond Crump Rukevwe
Raymond Crump Rukevwe - avatar
5 Answers
+ 3
sum=sum + myarr[x];
22nd Oct 2016, 10:43 AM
soundarya
+ 2
+= is ntg but sum = sum + myarr[x]; like this is a shortcut tht saves some amount of time and u can do this for other arithmetic operators like /= , -= , *= and %=
20th Oct 2016, 3:33 PM
Suhail Pappu
Suhail Pappu - avatar
+ 1
thanks
20th Oct 2016, 3:51 PM
Raymond Crump Rukevwe
Raymond Crump Rukevwe - avatar
0
sum=sum+myarr[x]
21st Oct 2016, 5:09 AM
jerry
jerry - avatar
0
'+=' will compound(will add up) each value 'X' returns to 'sum' every time the statement loops through.
3rd Nov 2016, 1:55 PM
Nanasei
Nanasei - avatar