< Previous | Contents | Next >

Summary

In this chapter, you learned the following concepts:

n You can use the truth or falsity of an expression to branch to (or skip) sections of code.

Summary 77


n You can represent truth or falsity with the keywords, true and false.

n You can evaluate any value or expression for truth or falsity.

n Any non-zero value can be interpreted as true, while 0 can be inter- preted as false.

n A common way to create an expression to be evaluated as true or false

is to compare values with the relational operators.

n The if statement tests an expression and executes a section of code only if the expression is true.

n The else clause of an if statement specifies code that should be executed only if the expression tested in the if statement is false.

n The switch statement tests a value that can be treated as an int and exe- cutes a section of code labeled with the corresponding value.

n The default keyword, when used in a switch statement, specifies code to be executed if the value tested in the switch statement matches no listed values.

n The while loop executes a section of code if an expression is true and repeats the code as long as the expression is true.

n A do loop executes a section of code and then repeats the code as long as the expression is true.

n Used in a loop, the break statement immediately ends the loop.

n Used in a loop, the continue statement immediately causes the control of the program to branch to the top of the loop.

n The && (AND) operator combines two simpler expressions to create a new expression that is true only if both simpler expressions are true.

n The || (OR) operator combines two simpler expressions to create a new expression that is true if either simpler expression is true.

n The ! (NOT) operator creates a new expression that is the opposite truth value of the original.

n The game loop is a generalized representation of the flow of events in a game, the core of which repeats.

78 Chapter 2 n Truth, Branching, and the Game Loop: Guess My Number


n The file cstdlib contains functions that deal with generating random numbers.

n The function srand(), defined in cstdlib, seeds the random number generator.

n The function rand(), defined in cstdlib, returns a random number.