Bifalgorithm post · Computer science one · While Loops
Why should you avoid a semicolon after a while condition?
Why should you avoid a semicolon after a while condition? (Computer science one · While Loops) — "Consider the following:\n\n1 while(i <= 10);\n\n2 {\n\n3 //pe…
Ideas in this flowchart
- Unintended infinite loop caused by a semicolon after while
- The while condition binds to the next executable statement or code block
- The semicolon makes the statement bound to while empty
- The code is effectively an empty while loop followed by a separate block
- The increment of i occurs outside the while loop
- The condition i <= 10 remains true because i is never incremented in the loop
- The loop does nothing and continues forever
- Some compilers warn about the problem, while others do not
- The code is valid C and compiles and runs, but does not work as intended
- Use proper syntax and good style to avoid the problem