Decisions, decisions, decisions. From the moment we are awake until the time
we go to sleep, we are making decisions. Should I eat cereal or toast? What
should I wear to school today? Should I eat at the cafeteria today? And so
forth. We make many of these decisions by evaluating some criteria. If the
number of students in line for registration seems long, then come back tomorrow
for another try. If today is Monday, Wednesday, or Friday, then eat lunch at the
cafeteria.
Computer programs are no different. Any practical computer program contains
many statements that make decisions. Often a course of action is determined
by evaluating some kind of a test (e.g., Is the remaining balance of a meal card
below the minimum?). Statements in programs are executed in sequence, which is
called sequential execution or sequential control flow. However, we can add
decision-making statements to a program to alter this control flow. For example,
we can add a statement that causes a portion of a program to be skipped if
an input value is greater than 100. Or we can add a statement to disallow the purchase
of food items if the balance of a meal card goes below a certain minimum.
The statement that alters the control flow is called a control statement. In this
chapter we describe some important control statements, called selection statements.
In Chapter 6 we will describe other control statements, called repetition
statements.
To learn more about the book this website supports, please visit its Information Center.