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


#1



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) {
	console.log(i);
}


#2

This sign,

<

And this sign,

==

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


#3

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


#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) {
	console.log(i);
}

without the original "<" ?


#5

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


#6

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


#7

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


#8

But why even with the

 i = i + 1

, it would not move up?


#9

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


#10

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