0

Js example

var count = 0; for(i=0; i<3; i++){ for(j=3; j>; j--){ ++count; } } alert(++count);//10. why output 10?

11th Mar 2019, 10:38 AM
Knozi
Knozi - avatar
7 Answers
+ 5
Ok..let me explain I loop First iteration: i=0 ,I<3 it's okay Then goes to j loop first iteration J=3 ,j>0 it's okay and ++count it's mean count=1 and j-- it's mean j=2 Then goes to j loop second iteration J=2 ,j>0 it's okay and ++count it's mean count=2 and j-- it's mean j=1 Then goes to j loop third iteration J=1 ,j>0 it's okay and ++count it's mean count=3 and j-- it's mean j=0 Now J=0 ,j>0 it's not okay finally j loop end Now again go to I loop second iteration: i=1 ,I<3 it's okay and so on as above Now it's run untill i=3 And finally count=9 If doesn't understand please study about Nested loops
11th Mar 2019, 11:48 AM
Scooby
Scooby - avatar
+ 6
Welcome.. no problem.. keep learning
11th Mar 2019, 12:15 PM
Scooby
Scooby - avatar
+ 5
Js example var count = 0; for(i=0; i<3; i++);{//are you sure here semi colon present? for(j=3; j>; j--){ ++count; } } alert(++count);//10. why output 10?
11th Mar 2019, 10:57 AM
Scooby
Scooby - avatar
+ 5
Js example var count = 0; for(i=0; i<3; i++){ for(j=3; j>; j--){ ++count; } } alert(++count);//10. The outer i loop control inner j loop.. That means I loop run 3times and j loop run 3times Now 3*3=9 times loops run ultimately and after loop exit count has value 9.. After that when it print it also increase to 10 That's why it's showing 10 Hope you understand 😉
11th Mar 2019, 11:27 AM
Scooby
Scooby - avatar
+ 1
Haven't sorry
11th Mar 2019, 11:21 AM
Knozi
Knozi - avatar
+ 1
Okay i go repeat nested loops thanks
11th Mar 2019, 11:59 AM
Knozi
Knozi - avatar
0
No i cant understand why in program do 3*3
11th Mar 2019, 11:36 AM
Knozi
Knozi - avatar