How to write if statements in C, with flowcharts for visual understanding.
if (cond) { ... } basic formelse handles the false caseelse if for three or more branches β strictest firstelse if for three or more caseselse binds in nested if statementsif does.if (condition) { // runs when condition is true }
int score = 75; if (score > 59) { printf("Pass\n"); }
score > 59 is true, the body inside { } runs. If score is 50, the condition is false and the body is skipped entirely.< and > to grasp the basic flow "true β enter, false β skip." Operators like ==, <=, >=, &&, and || are covered in the next lesson, "Comparison & Logical Operators."bool type (C99 added stdbool.h), so truth values are just integers.1, -1, 100, 3.14 ...if (1) // always true - body always runs if (0) // always false - body never runs if (score) // true when score is non-zero
{ } matter{ } group multiple statements into the if body. For a single-statement body, braces are technically optional β but beginners should always write them anyway.if (x > 0) { printf("positive\n"); count++; }
if (x > 0) printf("positive\n"); count++; // NOT inside if - always runs
count++; looks like it's inside the if, without braces only the very next statement counts as the body.else is the "otherwise" clause. When the if condition is false, the else body runs instead.int score = 45; if (score > 59) { printf("Pass\n"); } else { printf("Fail\n"); }
if body when true, the else body when false. Never both, and never neither.else is optionalelse isn't required. If you only care about the "true" case, just leave it off.if (age > 17) { printf("adult\n"); } else { printf("minor\n"); }
if (errors > 0) { printf("warning\n"); } // do nothing on success
if + else covers two outcomes. For three or more cases, chain else if. It's really just shorthand for "put another if inside the else."int score = 75; if (score > 89) { printf("A\n"); } else if (score > 69) { printf("B\n"); } else if (score > 49) { printf("C\n"); } else { printf("D\n"); }
score > 49 first, scores of 90 and 70 also match there, so they all stop at C. Always put the strictest (narrowest) condition first.else catches "none of the above." Omit it and nothing happens when no case matches β a silent no-op. Writing it explicitly is safer.; right after the conditionif (score > 59); { printf("Pass\n"); }
; right after the condition ends the if. The { ... } that follows is just a plain block, so it always runs regardless of score.if the else attaches toif (a > 0) if (b > 0) printf("both positive\n"); else printf("b is not positive\n");
else binds to the nearest if. To avoid confusion, always use { } when nesting.if (score > 49) { printf("C\n"); } else if (score > 89) { printf("A\n"); }
score > 49 is true, so it never reaches A. Remember: else if chains are evaluated top-down.int x = 100; if (2 < x < 5) { printf("in range\n"); }
2 < x first, gets 0 or 1, then compares that with < 5 — so even x = 100 comes out as "true". The proper way to write a range condition is covered in the next lesson, "Comparison & Logical Operators".| Name | Type | Value |
|---|
else if to grade: A/B/C/Dage to 16 or 20 and compare the outputx > 0 and x < 0if inside ifTest what you learned. To match the lesson, every problem here uses only < and >.
In C, 0 is false and anything non-zero is true. When the condition is 0, the if block is skipped.
int x = 5; if (x > 0) { printf("A\n"); } else { printf("B\n"); }
5 > 0 is true, so the if-branch prints A.
int x = -2; if (x > 0) { printf("positive\n"); } else { printf("not positive\n"); }
-2 > 0 is false, so the else branch runs.
int score = 75; if (score > 89) { printf("A\n"); } else if (score > 69) { printf("B\n"); } else { printf("C\n"); }
75 isn't greater than 89, but it is greater than 69, so B prints.
int score = 95; if (score > 49) { printf("C\n"); } else if (score > 89) { printf("A\n"); }
95 already satisfies the first score > 49, so C prints and the else if is never evaluated. Lesson: write the strictest condition first.
int score = 40; if (score > 59); { printf("Pass\n"); }
The stray ; after if (score > 59) ends the if right there. The block that follows has nothing to do with the if, so it always runs.
if (a > 0) if (b > 0) printf("both positive\n"); else printf("b is not positive\n");
else binds to the nearest if. The outer a > 0 is true and the inner b > 0 is false, so the else runs.
{ }In C, blocks are determined by { }, not indentation. To put multiple lines inside an if, always wrap them.