O'Reilly logo

C++ All-In-One For Dummies®, 2nd Edition by Jeff Cogswell, John Paul Mueller

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

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