summing of arry | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

summing of arry

what different between the 2 below example?how the output of first ex1 equal to 58 and how the output of second example equal 6,48,51,58 ex2: 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); } } ex2: 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); } } }

25th Sep 2018, 5:26 AM
Sanaa Hareb
Sanaa Hareb - avatar
1 Answer
+ 8
The first example will print the final sum after all steps of loop are done. The second one will keep printing the sum of every step because the print statement is written inside the loop. First code's output: 58 Second code's output: 6 48 51 58
25th Sep 2018, 5:38 AM
Shamima Yasmin
Shamima Yasmin - avatar