+ 2

Please explain this program

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); } }

15th Mar 2019, 2:20 AM
Priyanka Gotugade.
Priyanka Gotugade. - avatar
1 Answer
+ 6
Here, myArr has some elements and with for loop addition each element to others and Store it to sum variable.. myArr.length specific how array's length here is 4 After starting for loop x initialised to 0 and also check for x <4. As condition true loop run and Sum+=myarr[x] treat like sum=sum+myArr[0] Then sum=6 After that it's goes for x++ and loop run untill X value will be 4..
15th Mar 2019, 2:39 AM
Scooby
Scooby - avatar