< Previous | Contents | Next >

Questions and Answers

Q: Do you have to use the keywords true and false?

A: No, but its a good idea. Before the advent of the keywords true and false, programmers often used 1 to represent true and 0 to represent false. However, now that true and false are available, its best to use them instead of the old-fashioned 1 and 0.

Q: Can you assign a bool variable something other than true or false?

A: Yes. You can assign an expression to a bool variable, which will store the truth or falsity of the expression.

Q: Can you use a switch statement to test some non-integer value?

A: No. switch statements only work with values that can be interpreted as integers (including char values).

Q: How can you test a single non-integer value against multiple values if you cant use a switch statement?

A: You can use a series of if statements. Q: Whats an infinite loop?

A: A loop that will never end, regardless of user input. Q: Why are infinite loops considered bad?

A: Because a program stuck in an infinite loop will never end on its own. It has to be shut down by the operating system. In the worst case, a user will have to shut his computer off to end a program stuck in an infinite loop.

Q: Wont a compiler catch an infinite loop and flag it as an error?

A: No. An infinite loop is a logical errorthe kind of error a programmer must track down.

Questions and Answers 79


Q: If infinite loops are a bad thing, then isnt a while (true) loop a bad thing?

A: No. When a programmer creates a while (true) loop, he should provide a way for the loop to end (usually through a break statement).

Q: Why would a programmer create a while (true) loop?

A: while (true) loops are often used for the main loop of a program, like the game loop.

Q: Why do some people feel that using a break statement to exit a loop is poor programming?

A: Because indiscriminate use of break statements can make it hard to under- stand the conditions under which a loop ends. However, sometimes the use of a while (true) loop along with a break statement can be clearer than creating the same loop in a more traditional way.

Q: Whats a pseudorandom number?

A: A random number thats usually generated by a formula. As a result, a series of pseudorandom numbers is not truly random, but good enough for most purposes.

Q: Whats seeding a random number generator?

A: Its giving the random number generator a seed, such as an integer, which affects the way the generator produces random numbers. If you dont seed a random number generator, it will produce the same series of numbers each time its run from the beginning of a program.

Q: Dont you always want to seed the random number generator before using it?

A: Not necessarily. You might want a program to produce the exact same sequence of randomnumbers each time it runs for testing purposes, for example.

Q: How can I generate more truly random numbers?

A: There are third-party libraries that produce better pseudorandom numbers than the ones that typically come with C++ compilers.

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


Q: Do all games use the game loop?

A: The game loop is just a way of looking at a typical games flow of events. And just because this paradigm fits a particular game, that doesnt necessarily mean that the game is implemented with a loop around the bulk of its code.