Bifalgorithm post · Computer science one · While Loops
Why should a while-loop condition not end with a semicolon in PHP?
Why should a while-loop condition not end with a semicolon in PHP? (Computer science one · While Loops) — "The continuation condition does not contain a semico…
Ideas in this flowchart
- A semicolon after a while condition causes unintended loop behavior.
- The continuation condition has no semicolon because it is not an executable statement.
- As with an if-statement, adding a semicolon can lead to unintended results.
- Example: while($i <= 10); { $i++; }
- The while keyword and condition bind to the next executable statement or code block.
- Because of the semicolon, the statement bound to the while loop is empty.
- The action and $i++ are placed in the following code block.
- The loop therefore repeatedly executes the empty statement instead of the intended action.