+ 2

Can anyone explain this code? ( Mainly - {sum += myArr[x]; )

int [ ] myArr = {6, 42, 3, 7}; int sum=0; for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } System.out.println(sum); // 58

14th Mar 2018, 12:38 PM
Ratnesh Mishra
Ratnesh Mishra - avatar
3 Answers
+ 12
it simply sums up elements of that int array //starting from index 0 to lengthOfArray-1 [in loop] //sum+=x => sum=sum+x
14th Mar 2018, 12:50 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
welcome , statements execute from top to bottom available in block of main method()
14th Mar 2018, 1:27 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
Ohkk..Now I understand. But, tell me one last thing- The execution process of the statements . Means which statement will be executed first, then second and so on.. Thanks for answering.
14th Mar 2018, 1:23 PM
Ratnesh Mishra
Ratnesh Mishra - avatar