How to write if statements in C, with flowcharts for visual understanding.
if (cond) { ... } basic formelse handles the false casecond ? a : bif (condition) { // runs when condition is true }
int score = 75; if (score >= 90) { printf("Excellent\n"); } else if (score >= 70) { printf("Good\n"); } else if (score >= 50) { printf("Pass\n"); } else { printf("Fail\n"); }
| Name | Type | Value |
|---|
else if to grade: A/B/C/D== 0, >= 100)&& (e.g., age 18+ AND citizen)if inside ifCheck your understanding of this lesson!
In C, 0 is false and anything non-zero is true. When the condition is 0, the if block is skipped.
= is assignment, so 5 is stored into x. The result of the assignment is 5 (non-zero = true), so the branch always runs. This is a classic bug — you meant ==.
Each if can have only one else. Use else if for multi-way branching.
Learning the concepts here + doing exercises in a book is very effective
* These are affiliate links. Your purchases help support this site.