Control Structures

Control structures decide the order of execution: selection (if/switch) chooses among paths and iteration (for/while) repeats a block, with break and continue giving fine control over loop flow.

Key formulas & points

Skim these first — then read the full notes below.

  • if-else, for, while and do-while cover all structured control
  • break exits the loop; continue skips to the next iteration
  • The dangling-else binds to the nearest unmatched if

Topic details

Introduction

This topic covers how a program chooses and repeats. You trace conditional branches, count loop iterations precisely (including off-by-one boundaries), understand entry- versus exit-controlled loops, and resolve ambiguous constructs like the dangling-else through the nearest-if rule.

Key relations & formulas

Formulas (Indian textbook notation)

  • for(i=0;i<n;i++)niterationsfor (i = 0; i < n; i++) → n iterations

Formulas (Indian textbook notation)

  • inC,0=falseandanynonzero=truein C, 0 = false and any non-zero = true

Formulas (Indian textbook notation)

  • switch(x)compilestoajumptableoralinearcomparechainswitch(x) compiles to a jump table or a linear compare chain

Notation and sign conventions

Relation 1 —
forfor

Formulas (Indian textbook notation)

  • for(i=0;i<n;i++)niterationsfor (i = 0; i < n; i++) → n iterations
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
inC,0=falseandanynonzero=truein C, 0 = false and any non-zero = true

Formulas (Indian textbook notation)

  • inC,0=falseandanynonzero=truein C, 0 = false and any non-zero = true
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
switchswitch

Formulas (Indian textbook notation)

  • switch(x)compilestoajumptableoralinearcomparechainswitch(x) compiles to a jump table or a linear compare chain
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Structured programming replaces arbitrary jumps with three building blocks — sequence, selection and iteration — which together can express any algorithm. The distinction between a while loop (test first, may run zero times) and a do-while (test after, runs at least once) matters for edge cases. Loop bounds are the usual source of error: whether a condition uses < or <= changes the iteration count by one. switch statements are efficient when the compiler builds a jump table but degrade to sequential comparisons for sparse cases, and forgetting break causes fall-through to the next case.

Assumptions and validity limits

State assumptions explicitly before using any relation for control structures — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Programming Fundamentals viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Programming Fundamentals papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to control structures.
4. Use equation 1:
forfor
.
5. Use equation 2:
inC,0=falseandanynonzero=truein C, 0 = false and any non-zero = true
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Control Structures appears in all software development. In Indian it software curricula this topic is tested because it connects theory to core programming concepts in C/C++.
GATE and semester exams often combine control structures with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use control structures?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Off-by-one errors in loop bounds are the most common mistake, followed by missing break in switch causing fall-through, and mis-resolving the dangling-else. Students also confuse while (test-first) with do-while (test-after) for zero-iteration cases.

Quick revision checklist

Before attempting control structures problems, confirm you can:
1. if-else, for, while and do-while cover all structured control
2. break exits the loop; continue skips to the next iteration
3. The dangling-else binds to the nearest unmatched if
Revise the solved examples in Let Us C — Yashavant Kanetkar and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Loop iteration count

Problem

How many times does the body of `for (i = 2; i <= 10; i += 2)` execute?

Solution

i takes 2, 4, 6, 8, 10 — five values — so the body runs 5 times. Count the values satisfying the condition, not the range width.

Conceptual check — Control Structures

Problem

In a Programming Fundamentals semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of control structures." What should a complete answer include?

📖 Standard books (India)

  • Let Us CYashavant Kanetkar

    Read: Syllabus unit

    First programming book for many Indian students