0
Why in while loop we have 0 in output? Shouldn't it start with 1?
4 Answers
+ 1
There are two type of increment operator i=i+1
i++(post increament) or
++i(pre increament)
Post-increment operator used to increment the value of a variable after using it in a expression. In the Post-Increment, value is first used inside the expression.then increment
let i=0
X=i++ >>>> in short x=i=i+1( left to right precidence)
After execution
X= 0 and i=1
Means i is first assigned to x then it incremented
Pre-increment operator used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.
i=0
x=i++>>>>x=i=i+1(right to left precid.)
i is incresed to 1 and value of i assigned to x
After execution x=1 and i =1
+ 3
i++ is an operator which uses the current value of i before incrementing it, so document.write(i++) would output the current value of i (which, in the first iteration of the loop would be 0) and then increment it by 1
+ 1
var i=0;
while (i<10) {
document.write(++i);
}
0
Because you start it from 0 you can start from any number you wish and end it any number you wish but note that the condition is to be concerned
i.e while(condition(is still true))
do this
You may not use 0 or 1 the condition is just a short meaning for checking true or false
while the condition is True the code keeps executiing until it's false