Unable to understand this! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Unable to understand this!

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

20th Dec 2016, 5:26 PM
Puneet Thakur
Puneet Thakur - avatar
2 Answers
+ 2
This section below creates an integer array object with four elements inside which are 6, 42, 3, 7 respectively. int [ ] myArr = {6, 42, 3, 7}; This section below declares an integer variable with an initial value of zero but will change by the end of the for loop. int sum=0;// total sum of all numbers will be stored here This section below traverses through the array collection and adding each individual element to the "sum" variable above. for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } Finally, if you still don't understand it then I recommend codingbat website ( a lot of exercises to get you going ), reading relevant books, get involved in programming communities. STUDY, PRACTISE AND PERSISTENCE! HAPPY CODING!.
20th Dec 2016, 5:51 PM
Ousmane Diaw
+ 1
int [ ] myArr = {6, 42, 3, 7};//Array int sum=0;// Sum for(int x=0; x<myArr.length; x++) { sum += myArr[x];}// sum = 0 + 6, sum = 6 +42, sum = 48 +3 ...
20th Dec 2016, 5:37 PM
Alikhan Suleimen
Alikhan Suleimen - avatar