liveBook · Manning

Conditional logic

As explained in Modern C

A conditional statement selects whether and which dependent statement or block executes based on a condition.

Consider a complete decision in C: if (i > 25) { j = i - 25; } else { j = i; }. The controlling expression selects one dependent statement or block, so execution follows exactly one of two paths. section 3.1

   if (i > 25) {
     j = i - 25;
   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":5}]]"}
!@%STYLE%@!

Read the compact form from the outside in: if introduces the condition, parentheses contain the controlling expression, and braces delimit the dependent block. section 3.1

Keep these rules visible when writing or reading conditional logic:

  • An if statement runs its dependent statement or block once when the controlling expression is true, and not at all otherwise. section 3.1
  • Adding else supplies the alternative path; the if ... else construct selects one of two code paths. section 3.1
  • C treats 0 as logical false and every nonzero numerical value as logical true, so if (i) is equivalent in intent to if (i != 0). section 3.1

Skipping the first element

for (size_t i = 0; i < 5; ++i) { if (i) { printf("element %zu ...", i); } } The loop iterates over the range, while the if prevents output when i is zero. section 3.1

How Modern C covers it

How deep it goes
Defines syntax, branch behavior, numerical truth rules, and comparisons while deferring complex condition primitives.
Examples
Uses threshold branching, an if-else assignment, and a loop that suppresses output for zero.
What it assumes
Assumes basic C statements and iteration, while defining controlling expressions, dependent blocks, and numerical truth from examples.
What you can do after
Apply conditional execution in C using comparisons, numerical conditions, and alternative branches.
How it connects
Connects conditional logic with Boolean expressions, for loops, switch statements, and the ternary operator.

Also explained in

Compare how 21 books explain conditional logic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.