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