Why can't I replace "<" with "==" in the loops?


When I use this code, the program doesn't run. Why?
Is there a way to rewrite the (<) as (==)?

for (var i = 4; i == 24; i = i + 1) {


This sign,


And this sign,


mean totally different things that is why you can't replace this < with this ==.


the code will never run because of that code. Because it will always be false since you declared i to have a value of 4


But why can't it understand my code as moving up (by i = i + 1)?

So how would you rewrite this code

for (var i = 4; i == 24; i = i + 1) {

without the original "<" ?


you could use <=? The loop will run as long as the condition is true, this condition: i == 24 is false, so the loop will never run


What about "="? When I ran that code with a "=" the program crashed.


using = will assign 24 to i, then js will evaluate if 24 is true, which it is, and then the condition never becomes false, you will have an infinity loop, and you experienced the consequences of a infinity loop


But why even with the

 i = i + 1

, it would not move up?


and then here: i = 24, you will set i to 24 again, then you increase it to 25, then it will be set to 24 again and so on until infinity, positive integers will evaluate to true


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.