Chapter 3. Directing Your C++ Program Flow

In This Chapter

  • Comparing numbers and evaluating other conditions

  • Doing things based on a comparison

  • Repeating code a certain number of times

  • Repeating code while certain things are true

  • Repeating code that repeats code that ...well, you get the idea

As you program in C++, many times you need to present the computer with a choice, allowing it to do one thing for one situation and something else in another situation. For example, you may have a program that asks for a user's password. If the password is correct, the program continues; but if the password is incorrect, the program asks the user to reenter the password. After some number of times, usually three, the program performs yet another task when the user enters the incorrect password. Such situations are called conditions. In the case of the password, the condition is whether the password matches.

You may also encounter situations where you want several lines of code to run over and over. These are called loops, and you can specify conditions under which the loop runs. For example, you may want to check the password only three times; and if the user fails to enter it correctly on the third time you may bar access to the system. This would be a loop, and the loop would run under the condition that a counter has not exceeded the value of 3.

In this chapter, we take you through different ways to evaluate conditions within your programs and cause different sections of code to run based on those ...

Get C++ All-In-One For Dummies®, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.