C if Statement: if, else if, else
if runs the statement right after it (or a block in braces) only when the condition in the parentheses is true. Add else to say what to do when it is false, or else if to go on to another condition.
if (condition1) {
when condition1 is true
} else if (condition2) {
when condition1 is false and condition2 is true
} else {
when all are false
}
if and else
Always wrap the condition in parentheses. For comparisons, use == (equal), != (not equal), <, >, <=, and >=. You can leave out the else.
#include <stdio.h> int main(void) { int n; scanf("%d", &n); if (n % 2 == 0) { printf("even\n"); } else { printf("odd\n"); } if (n > 5) { printf("big\n"); } return 0;}7
odd big
Several branches with else if
The conditions are checked in order from the top. Only the one branch whose condition is true first runs, and the rest are skipped. So in a lower branch you don't need to repeat the condition above it, as in t >= 10 && t < 20.
#include <stdio.h> int main(void) { int t; scanf("%d", &t); if (t < 10) { printf("cold\n"); } else if (t < 20) { printf("cool\n"); } else if (t < 28) { printf("warm\n"); } else { printf("hot\n"); } return 0;}23
warm
Combining conditions: &&, ||, !
&& is true when both sides are true, and || is true when at least one is. ! flips true and false. If the left side alone settles the answer, the right side is not evaluated. That makes n != 0 && 100 / n > 10 safe: when n is 0, the division doesn't happen.
#include <stdio.h> int main(void) { int age = 9, height = 118, day = 7, n = 0; if (age >= 8 && height >= 120) printf("ride ok\n"); else printf("ride no\n"); if (day == 6 || day == 7) printf("weekend\n"); if (!(age >= 13)) printf("child\n"); if (n != 0 && 100 / n > 10) printf("small n\n"); else printf("skip\n"); return 0;}ride no weekend child skip
In C, true and false are numbers
A condition in C is false if it is 0 and true for anything that is not 0. The result of a comparison is a number too: 1 when it is true and 0 when it is false. So if (count) means the same as if (count != 0).
#include <stdio.h> int main(void) { int count = 0, left = -3; printf("%d %d\n", 3 > 2, 3 < 2); if (count) printf("has items\n"); else printf("empty\n"); if (left) printf("not zero\n"); return 0;}1 0 empty not zero
A negative number is not 0, so it is true as well. If you want to use the words true and false, add #include <stdbool.h>.
Common mistakes
Writing = where you mean ==
#include <stdio.h>
int main(void) {
int x = 3;
if (x = 5) printf("five\n");
printf("%d\n", x);
return 0;
}
→ x is 3, yet five is printed, and x changes to 5 as well
= is assignment, not comparison. x = 5 stores 5 in x, and that value 5 (not 0, so true) becomes the condition. To compare, write x == 5. If you turn on -Wall, the compiler warns you about places like this.
Writing two lines without braces
#include <stdio.h>
int main(void) {
int score = 40;
if (score >= 60)
printf("pass\n");
printf("good job\n");
return 0;
}
→ The score fails, yet good job is printed
Without braces, only the one statement that comes next belongs to the if. Indentation means nothing. Get into the habit of always using { }, even for a single line.
Putting a semicolon after the condition
#include <stdio.h>
int main(void) {
int n = -1;
if (n > 0);
{
printf("positive\n");
}
return 0;
}
→ n is negative, yet positive is printed
The semicolon in if (n > 0); becomes a "statement that does nothing", and the if ends there. The braces after it always run, whatever the condition is. Remove the ; after the parentheses.
Chaining comparisons like 1 < x < 10
int x = 50;
if (1 < x < 10) printf("in range\n");
else printf("out of range\n");
→ x is 50, yet in range is printed. Recent compilers warn about this expression
It is evaluated from the left: 1 < x becomes 1 (true) first, and then 1 < 10 is checked, which is always true. For a range, join two comparisons with &&, like 1 < x && x < 10.