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]?
5 Réponses
+ 3
sum=sum + myarr[x];
+ 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 %=
+ 1
thanks
0
sum=sum+myarr[x]
0
'+=' will compound(will add up) each value 'X' returns to 'sum' every time the statement loops through.