Why this code give answer 9 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this code give answer 9 ?

var count=0; for(var i=0;i<3;i++){ for(var j=0;j<=2;j++){ count++; } } Document.write(count);

24th Jul 2020, 6:35 PM
ILLUMINATIE
ILLUMINATIE - avatar
2 Answers
+ 2
Outer loop runs for i =0,1,2 ; so 3 times iterates *(multiply) (Inner loop runs 3 times j = 0,1,2 so increments count 3 times.) Hence, 3*3 =9 times count++ executes so count=9. Is it OK now? What is your answer?
24th Jul 2020, 6:55 PM
Jayakrishna 🇮🇳
0
Initially cout = 0 When i=0 J= 0, 1 ,2 Cout = 1 1 1 Now cout =0 + 3 = 3 When i=1 J= 0, 1 ,2 Cout = 1 1 1 Now cout = 3 + 3 = 6 When i=2 J= 0, 1 ,2 Cout = 1 1 1 Now cout = 6 + 3 = 9
24th Jul 2020, 7:37 PM
David