Chapter 3. Decisions and Loops
WHAT YOU WILL LEARN IN THIS CHAPTER:
How to compare data values
How to alter the sequence of program execution based on the result
How to apply logical operators and expressions
How to deal with multiple choice situations
How to write and use loops in your programs
In this chapter, you will look at how to add decision-making capabilities to your C++ programs. You'll also learn how to make your programs repeat a set of actions until a specific condition is met. This will enable you to handle variable amounts of input, as well as make validity checks on the data that you read in. You will also be able to write programs that can adapt their actions depending on the input data, and that can deal with problems where logic is fundamental to the solution.
I'll start with one of the most powerful and fundamental tools in programming: the ability to compare variables and expressions with other variables and expressions and, based on the outcome, execute one set of statements or another.
Unless you want to make decisions on a whim, you need a mechanism for comparing things. This involves some new operators called relational operators. Because all information in your computer is ultimately represented by numerical values (in the last chapter you saw how character information is represented by numeric codes), comparing numerical values is the essence of practically all decision making. You have six fundamental operators for comparing two values available: ...