O'Reilly logo

Ivor Horton's Beginning Visual C++® 2010 by Ivor Horton

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

COMPARING VALUES

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: ...

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