The if Statement

The if statement performs a branch based on the outcome of a conditional test. If the test expression is true, the body of the if statement executes. If it is false, the statement body is skipped.

The else keyword is used with if to form an either-or construct that executes one statement when the test expression is true and another when it's false. C does not offer an “else-if” keyword. You can combine if and else statements to achieve the same effect. C pairs each else with the most recent if that lacks an else.

Below is a simple if statement:

if( score < 70 )

grade = 'F';

else

grade = 'P';

If the value of the variable score is less than 70, the variable grade is set to the constant F. Otherwise, score is set to P.