Professional Documents
Culture Documents
5 CS1PR16 Conditional Statements
5 CS1PR16 Conditional Statements
IF … ELSE
If may be supplemented by an else statement – the else part will be executed if the condition is false.
Example:
if (grade>=60)
{printf("Passed");}
else
{printf("Failed");}
If the block of code to be executed consists of only one line, the braces may be skipped. The
ternary operator may also be used in this case:
Grade >= 60 ? printf("passed") : printf("failed");
However, this is not generally recommended as it decreases clarity.
You can nest if...else statements inside each other but you shouldn't have too many of them.
Confusing equality (==) and assignment (=) operators is a common mistake. It doesn't usually cause a
syntax error and still compiles but leads to incorrect results.