-2

I've been experimenting with for loops, mostly with a condition 'less than or equal'. However, I wanted to make the condition of a for loop so that the number in the initialization is bigger than that one in the condition. When I ran the code, it didn't work. And when I added the bigger than or equal operator, it crashed; here's the code:

//loop n1
for (let i = 0; i > 4; i++) {
    console.log(i);
}

//loop n2
for (let i = 0; i >= 4; i++) {
    console.log(i);

//loop n3
for (let i = 0; i = 4; i++) {
    console.log(i);

None of these worked, and the last one crashed. I don't get it; according to my logic, loop n3 should have stopped when "i" is equal to 4, but no, it crashed. And loop n1 should've stopped when "i" is more than 4.

Can anybody explain this to me?

Thanks

  • `n3` should be `=== 4`. When you use a single `=` you are assigning 4 to `i`. Instead, if you want to compare equality, you should use `===`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness – sagar1025 Aug 01 '21 at 16:41
  • Even if replaced with an appropriate equality comparison n3 will never run as it will never fulfill the condition and for loops only run when the condition returns true. – pilchard Aug 01 '21 at 16:48
  • 1
    [In javascript == vs =?](https://stackoverflow.com/q/11871616) – VLAZ Aug 01 '21 at 16:48
  • "*And loop n1 should've stopped when "i" is more than 4.*" the condition in the `for` loop has to return `true` for the loop to execute. `0 > 4` is `false`. As is `0 >= 4` – VLAZ Aug 01 '21 at 16:50
  • Yes, I understand the OPs code is an infinite loop, I mean if replaced with an appropriate equality comparison operator it will never run. – pilchard Aug 01 '21 at 16:50

2 Answers2

1
for will run while condition is true

// will exit right away, i is smaller then 4
for (let i = 0; i > 4; i++) {
    console.log(i);
}

// will exit right away, i is smaller then 4
for (let i = 0; i >= 4; i++) {
    console.log(i);
}

// will exit right away, i is smaller then 4
for (let i = 0; i === 4; i++) {
    console.log(i);
}
Grumpy
  • 2,140
  • 1
  • 25
  • 38
0

The issue you're encountering is that the condition needs to resolve to "true" in order for the loop to continue. Let's run through your examples...

For the first one, you start with i is equal to zero (0), and then test for i being greater than four (4). Well, zero (0) is less than four (4). That immediately fails and the loop does not execute.

For the second one, again you start with i equal to zero (0), and your condition of i being greater than or equal to four (4) still fails.

In your third example, it's still pretty much the same as the previous two because zero (0) is not equal to four (4).

For your for loops to successfully execute, the condition statement has to return "true" for at least the first value that your iterator (in this case the variable i) is set to. If you want to try a loop that works, for the first two examples you can change the "greater than" (> / >=) to "less than" (< / <=) and then it will run. Or, if you change the value of i to be four (4) or greater than four, and change your incrementing statement from "i++" to "i--", that would also allow it to run. For your final example, something like that would be far better off being done as an if statement instead of a for loop.

Michael McCauley
  • 853
  • 1
  • 12
  • 37
  • 1
    @santti_alter they show a lack of research. In the future read the documentation first, search for similar questions, try debugging the code yourself, and then finally ask question. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement) is a great resource with extensive examples. – pilchard Aug 01 '21 at 17:25