Ch 6 · Conditional Statements · C

Topic 6 of 13 in C — Complete Tutorial (Beginner) — 2 lessons.

if / else if / else

A conditional statement lets your program take different actions depending on a condition. You write if, then a condition in round brackets, then a block in curly braces that runs only when the condition is true (non-zero). Add else if for more options and a final else as the catch-all. C checks the conditions top to bottom and runs the first true one, skipping the rest.

switch and the Ternary Operator

When comparing one variable against many specific values, a switch is cleaner than a long else if chain. Each case needs a break to stop "falling through" to the next; default is the catch-all. For a quick two-way choice that produces a value, the ternary operator condition ? a : b is a compact one-line if-else.

All topics in C Beginner

  1. Introduction to Programming & C
  2. Your First C Program
  3. Variables & Data Types
  4. Input & Output
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Arrays
  10. Strings
  11. Pointers
  12. Structures
  13. Mini-Projects & Practice