How to make decisions with if/else
Write an if statement
C treats zero as false and nonzero as true. Braces make the controlled block unambiguous.
if (temperature > 25) {
printf("It is warm.\n");
}
Add alternatives
Conditions are tested in order. Only the first matching branch runs.
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else {
grade = 'C';
}
Combine conditions
Use &&, ||, and !. Short-circuit evaluation can guard a later operation.
if (divisor != 0 && total / divisor > 10) {
printf("Large quotient\n");
}
Avoid accidental assignment
Use == for comparison. Strong compiler warnings usually flag suspicious assignments inside conditions.
if (status == 0) {
printf("Success\n");
}