C++ Book


Questions and Answers

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

A: No, but it’s a good idea Before the advent of the keywords ‘true’ and ‘false’, programmers often used to represent true and 0 to represent lale. However, now that ‘true’ and ‘false’ are available, it’s best to use them instead of the old-fashioned I 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 integers (including ‘char’ values).

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

A: You can use a series of ‘if’ statements.

Q: What’s 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. Ik to be shut down by the operating system. In the worst case, a user will have shut his computer off to end a program stuck in an infinite loop.

Q: Won’t a compiler catch an infinite loop and flag it as an error?

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

Q: If infinite loops are a bad thing, then isn’t 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 understand the conditions under which a loop break. 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: What’s a pseudorandom number?

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

Q: What’s seeding a random number generator?

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

Q: Don’t 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 “random” numbers 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.

Q: Do all games use the game loop?

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


Discussion Questions

  1. What kinds of things would be difficult to program without loops?
  2. What are the advantages and disadvantages of the ‘switch’ statemnet versus a series of ‘if’ statement?
  3. When might you omit a ‘break’ statement from the end of a ‘case’ in a ‘switch’ statement?
  4. When should you use a ‘while’ loop over a ‘do’ loop?
  5. Describe your favorite game in terms of the game loop. Is the game loop a good fit?