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.
Feed/

Why should a while-loop condition not end with a semicolon in PHP?

No cover

Channel

Computer science one

1 page

"The continuation condition does not contain a semicolon since it is not an executable statement. Just as with an if-statement, if we had placed a semicolon it would have led to unintended results. Consider the following:\n\n1 while($i <= 10); {\n\n2 //perform some action\n\n3 $i++; //iteration\n\n4 }\n\nA similar problem occurs. The while keyword and continuation condition bind to the next executable statement or code block. As a consequence of the semicolon, the executable statement that gets bound to the while loop is empty."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.