C++ Book


Chapter 2 - Truth, Branching, and the Game Loop: Guess My Number.

You’ll create more interesting games by writing programs that execute, skip, or repeat sections of code based on some condition. You’ll learn how to generate random numbers to add some unpredictability to your games. And you’ll learn about the Game Loop—a fundamental way to organize your games to keep the action going.

So far, the programs you’ve seen have been linear—each statement executes, in order, from top to bottom. However, to create interesting games, you need to write programs that execute (or skip) sections of code based on some condition. That’s the main topic of this chapter. Specifically, you’ll learn to:

  • Understand truth (as C++ defines it)
  • Use if statements to branch to sections of code
  • Use switch statements to select a section of code to execute
  • Use while and do loops to repeat sections of code
  • Generate random numbers

Resources

Programs