Skip to content

Electronics · Ch 12 — C Programming

Chapter Summary

Chapter Summary

This part of the C Programming chapter covers how a program controls the order in which its statements run, and the tools it uses to make decisions, repeat work, organise code and picture logic.

  • By default a C program runs sequentially; control statements change that order and are of two kinds — conditional (selective) and iterative (looping).
  • Conditional control statements make a decision from a logical test and branch accordingly:
    • if — one-way branching; run a block only when a condition is true.
    • if-else — two-way branching; choose between two blocks.
    • nested-if (an else-if ladder) — choose among more than two alternatives.
    • switch — multiple-way branching on an int or char value, using case labels, break, and an optional default.
  • Iterative (loop) statements repeat a body until a condition is met; each loop has a body and a control statement:
    • while — a pre-test loop (condition checked first, so the body may run zero times).
    • do-while — a post-test loop (body runs at least once, condition checked last).
    • for — used when the repeat count is known; combines initialization, test and update; the number of iterations is (Final value − Initial value + step) ÷ step.
    • break exits a loop or switch case; continue skips the rest of the current pass and continues with the next iteration.
  • A function performs one task and returns a single value; functions are standard (library) — such as sqrt, cbrt, log, sin, pow — or user-defined sub-programs written before or after main().
  • Header files declare library facilities: conio.h (console I/O and screen clearing), stdio.h (standard input/output, nearly a third of the library), and math.h (mathematical functions).
  • Flowcharts picture program logic with standard symbols — parallelogram (input/output), rectangle (processing), diamond (decision), rounded rectangle (start/stop), circle (connector) and arrow (flow direction). …