Bifalgorithm post · Computer science one · If, If-Else, If-Else-If Statements
What happens when a semicolon is placed after an if condition in C?
What happens when a semicolon is placed after an if condition in C? (Computer science one · If, If-Else, If-Else-If Statements) — "Suppose we made a mistake an…
Ideas in this flowchart
- Unintended semicolon after an if condition changes the behavior of valid C code.
- The conditional is written as if(x < 10);
- The code is valid C and will compile and run, although some compilers may warn.
- An if statement binds to the executable statement or code block immediately following it.
- The semicolon supplies an empty executable statement for the if statement.
- The following printf block is separate from the if statement.
- Because x equals 15, the condition x < 10 is false.
- The separate block nevertheless prints "x is less than 10".
- The code is effectively equivalent to an if statement containing an empty block followed by the printing block.