+ 3
why this is not printing 0?
6 Answers
+ 2
Because you said i>0 me ans until i is greater than zero. you can use i>=0 then it will be correct.
Let me explain in deep:
result = [];
for (i = 4; i > 0; i -= 2){
result.push(i);
}
I = 4, I is greater than zero so i-2
I = 2, I is greater than zero so
I-2
I = 0, but 0 is not bigger than 0 so loop will end
+ 1
Because you write:
i > 0
But this will not print 0 because (i) is greater than 0
For solving this problem just add:
i >= 0
Now (i) is equal or greater than 0 and it will print 0 too.
+ 1
I working like this
I=4>0;
Print 4 //in result
now I= 2
I=2>0
Print 2 //in result
Now I=0
I=0 not >0
Hence no print.//in result
hope I clear my view to u.
0
Because condition is I>0
Use I >= 0 to get 0 also
0
Divya Mohan but, when
i = 2, 2 > 0, so i = 2-2
so i becomes 0
then
i = 0, 0>0 (false) so it wont go for 3rd condition.
but i value still remains 0 right ?
0
so it will push the value to the array only if second condition is true?