liveBook · Manning

Conditional logic

As explained in Modern C, Third Edition

An if-else statement conditionally selects between alternative statements based on a controlling expression.

Conditional logic selects which statements execute by evaluating a controlling expression. In C, an if statement runs its secondary block once when the condition is true; an if–else statement selects exactly one of two code paths. section 3.0 section 3.1

Selecting a code path

if (i > 25) { j = i - 25; } else { j = i; } The expression i > 25 controls the choice. The first block executes when it is true; otherwise the else block executes. section 3.1

C conditions follow these rules:

  • Zero represents logical false; every nonzero value represents logical true. section 3.1 section 3.4
  • Use == and != to test equality and inequality; a numeric value such as i can also serve directly as the condition. section 3.1

Conditional logic also connects to related control constructs. Loops repeatedly test conditions, while switch performs multiple selection on an integer value. The conditional operator, written cond ? A : B, provides expression-level selection and evaluates only the chosen branch. section 3.0 section 4.5

How Modern C, Third Edition covers it

How deep it goes
Defines syntax, branch behavior, truth rules, and comparisons, while deferring the full set of condition primitives.
Examples
Uses comparisons, direct numeric conditions, Boolean simplification, loop filtering, and conditional expressions selecting values or safe function arguments.
What it assumes
Assumes basic C statements and expressions, while teaching controlling expressions, truth interpretation, secondary blocks, and selection.
What you can do after
Apply C conditions to choose statements, simplify Boolean tests, and select values without evaluating an unused branch.
How it connects
Connects conditional execution with control flow, Boolean expressions, iteration statements, switch statements, and the conditional 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, Third Edition. The section links point to the parts of the book it draws on.