Chapter 5

Program Control

Up to this point we have seen that program’s statements are executed from top to bottom, in the order that they appear inside the source code. However, in real programming, certain statements should be executed only if specific conditions are met. This chapter will teach you how to use if and switch selection statements to control the flow of your program and make it select a particular execution path from a set of alternatives. It also introduces the conditional operator (?:), which can be used to form conditional expressions.

if Statement

The if statement controls program flow based on whether a condition evaluates to true or false. The simplest form of if statement is

if(condition)
{
… /* block of statements. ...

Get C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.