3 More Flow of Control

When you come to a fork in the road, take it.

ATTRIBUTED TO YOGI BERRA

Introduction

The order in which the statements in your program are performed is called flow of control. The if-else statement, the while statement, and the do-while statement are three ways to specify flow of control. This chapter explores some new ways to use these statements and introduces two new statements called the switch statement and the for statement, which are also used for flow of control. The actions of an if-else statement, a while statement, or a do-while statement are controlled by Boolean expressions. We begin by discussing Boolean expressions in more detail.

Prerequisites

This chapter uses material from Chapter 2.