< Previous | Contents | Next >

Truth, Branching, and the Game Loop: Guess My Number


So far, the programs youve seen have been lineareach 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. Thats the main topic of this chapter. Specifically, youll learn to:

n Understand truth (as C++ defines it)

n Use if statements to branch to sections of code

n Use switch statements to select a section of code to execute

n Use while and do loops to repeat sections of code

n Generate random numbers


 

Understanding TruthUsing the if StatementIntroducing the Score Rater ProgramTesting true and falseInterpreting a Value as true or falseUsing Relational OperatorsNesting if StatementsUsing the else ClauseIntroducing the Score Rater 2.0 ProgramCreating Two Ways to BranchUsing a Sequence of if Statements with else ClausesIntroducing the Score Rater 3.0 ProgramUsing the switch StatementIntroducing the Menu Chooser ProgramCreating Multiple Ways to BranchUsing while LoopsIntroducing the Play Again ProgramLooping with a while LoopUsing do LoopsIntroducing the Play Again 2.0 ProgramLooping with a do LoopUsing break and continue StatementsIntroducing the Finicky Counter ProgramCreating a while (true) LoopUsing the break Statement to Exit a LoopUsing the continue Statement to Jump Back to the Top of a LoopUnderstanding When to Use break and continueUsing Logical OperatorsIntroducing the Designers Network ProgramUsing the Logical AND OperatorUsing the Logical OR OperatorUsing the Logical NOT OperatorUnderstanding Order of OperationsGenerating Random NumbersIntroducing the Die Roller ProgramCalling the rand() FunctionSeeding the Random Number GeneratorCalculating a Number within a RangeUnderstanding the Game LoopIntroducing Guess My NumberApplying the Game LoopSetting Up the GameCreating the Game LoopWrapping Up the GameSummaryQuestions and AnswersDiscussion QuestionsExercises