O'Reilly logo

C by Nikolaos D. Tselikas, George S. Tselikis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. */ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required