+ 3

var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); the output is 16.... can a

12th Oct 2016, 11:43 AM
Shabana.A
Shabana.A - avatar
22 odpowiedzi
+ 42
i hate life
11th Dec 2018, 1:28 PM
kristina
kristina - avatar
+ 13
4+5+(skip 6 because of "continue")+7=16
14th Oct 2016, 8:56 AM
yugor
+ 11
Hi! I will difinitely check it step by step on http://www.pythontutor.com/live.html#mode=edit I just added this code to make it run: let sum=0; let i=4; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); It will show you step by step how it works and why it gives 16! I was also crazy about this answer, so here is the answer: When it do the 1st "for" loop, it comes out "4", so the "sum +=i" now values 4, then the "4" stays/sits on the sum waiting for the next itineration, this will be "5", now the trick is that the += means that it will add itself using the prior value, which was "4", this means that 4 + 5 = 9! NOw "sum" valu is 9. 6 is ignored, and here comes the 7! So now 7 + 9 = 16! I know is kind of messy from my side, but hopes it helps someone.
9th Mar 2020, 2:51 PM
Oscar
+ 3
I just added this code to make it run: let sum=0; let i=4; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); It will show you step by step how it works and why it gives 16! I was also crazy about this answer, so here is the answer: When it do the 1st "for" loop, it comes out "4", so the "sum +=i" now values 4, then the "4" stays/sits on the sum waiting for the next itineration, this will be "5", now the trick is that the += means that it will add itself using the prior value, which was "4", this means that 4 + 5 = 9! NOw "sum" valu is 9. 6 is ignored, and here comes the 7! So now 7 + 9 = 16! I know is kind of messy from my side, but hopes it helps someone.
22nd Jul 2020, 4:01 AM
Farhad Rahmani
Farhad Rahmani - avatar
+ 2
The tricky part is that the "document.write()" is outside of he loop. So after "for..." is looped all around, only then comes the printed result with the last possible "i" in the "for" loop i.e 7. If you simply put the "document.write()" before the last "}" of the loop it will print what all of us - the beginners expected before thinking it through. P.S don't forget the + "<br />" so the print is easier to look at: var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; document.write(sum + "<br />"); }
29th Oct 2019, 7:38 PM
Peter Nedyalkov
Peter Nedyalkov - avatar
+ 2
answer theory 1. (sum declared as 0) sum = (0 + next iteration of loop) --outside of loop 2. (sum declared as 4) sum = (4 + next iteration of loop) 3. (sum declared as 5) sum = (4+5+next iteration of loop) 4. (skip 6) 5. (sum declared as 7) sum = (4+5+7) 6. stop loop because 8 is equal to 8 not less than 7. output = (0 + 4 + 5 +7) = 16
18th Jan 2022, 7:53 AM
Future
+ 1
you kristina keep going
15th May 2019, 6:49 PM
Enrico Rico Tigani
Enrico Rico Tigani - avatar
+ 1
16
6th Aug 2019, 6:51 PM
PRATIK NAVALE
+ 1
so if 4 =i++ equals 6 it ignores it moves on to the next number. but if i=4; i<8; if i==6; wouldnt you take 6+8+2 to get the answer.
13th Dec 2020, 2:37 AM
Travis Troy Knutson
Travis Troy Knutson - avatar
+ 1
1054 Comments What’s the output of this code? var sum=0; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); 16 Ans:- First checking for loop, (i=4; 4<8; 5), (i=4; 4<8; 6), (i=4; 4<8; 7). Now, got our for loop result. next step is if condition (5==6) (6==6) (7==6). Now here is 7 and 5 is not == 6, so we have now 5 and 7 Okay!. next step is if == 6 thn continue otherwise sum+=i means add it by sum value okay!. and sum values are = 4,5,7. so adding now= (0+4+5+7) = 16 . I hope it will help you.
7th Jan 2021, 12:14 PM
Priya Ghosh
Priya Ghosh - avatar
+ 1
16
10th Aug 2021, 9:33 PM
W.G.A.C.Nandasena
W.G.A.C.Nandasena - avatar
0
thx
25th Dec 2017, 10:26 AM
Robby Mugi Jatmika
Robby Mugi Jatmika - avatar
0
Kristina, please can't you put a comment like this here. thx
31st Jan 2019, 12:22 PM
Frederik Declercq
Frederik Declercq - avatar
0
I Things its total of 4+5+7 = 16
8th Sep 2020, 2:48 PM
Nikul Goyani
Nikul Goyani - avatar
0
16
24th Mar 2021, 4:39 PM
Hesam Dehghan Moshtaghin
0
jovob 16
7th May 2021, 7:10 AM
Nozim Azamov
Nozim     Azamov - avatar
0
I got 16 and still can't explain it. This is so hard, but I'll get it though.
11th Jun 2021, 11:20 AM
Michael Mogor
0
It seems so tricky though!! I just added this code to make it run: let sum=0; let i=4; for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } document.write(sum); Let me show you how it becomes 16! It so funny about this answer, so here is the answer: When it do the 1st "for" loop, it comes out "4", so the "sum +=i" now values 4, then the "4" remains on the sum waiting for the next iteration, this will be "5", now the trick is that the += means that it will add itself using the prior value, which was "4", this means that 4 + 5 = 9! Now "sum" value is 9. 6 is ignored, and here comes the 7! that's why, now 7 + 9 = 16!
26th Jul 2021, 11:30 PM
Rajan Raj Uprety
0
8th Feb 2022, 8:17 PM
EL KHAYAOUI MOHAMED AMINE
0
16
31st Jul 2022, 2:15 PM
Kelly Jhojana Gomez Pernia