Programming Exercises


Extend the program of Listing 9.13 to include the following features:

  1. Instead of choosing from only four letters (A, B, C, and D) when generating the letterCode, let the program choose between the five letters A, B, C, D, and E.

  2. If the user could not guess the letterCode after 5 guesses, let the program reveal the letter held in position zero.

  3. If the user did not detect the letterCode after 7 guesses, let the program reveal the combination and end the game.

  4. Let the program keep track of the number of guesses required for each game so that the average number of guesses can be printed out just before the program is terminated.

  5. Let the program keep track of the maximum and minimum number of guesses of any one game. Print these statistics at the end of a game.

  6. Enable the program to play with three letters instead of two.

Note: No answers are provided for these programming exercises.

Keep Variable Declarations Outside the Loop Body If Possible


None of the iteration statement examples presented in this section contain any declaration statements inside their loop body. Instead, they are positioned outside, usually at the beginning of the method. This is not coincidental. Declaration statements residing inside a loop body will, like any other statement, be executed repeatedly. This can, depending on the inner workings of the compiler, slow down the program because variables can be created, destroyed, and recreated every time the loop is repeated.



Unintentional use of the empty statement with loop statements.

The empty statement is, as you might recall, conceived when a semicolon is placed where a statement is expected. We saw in the previous chapter how it can cause trouble for the if statement. Unfortunately, it might cause exactly the same type of problem for the while and for constructs as shown next, using the while statement as an example:


The compiler can detect the unintentional empty statement and report a warning with the following message:

 EmptyStatement.cs(8,22): warning CS0642: Possible mistaken null statement 

Intentional Use of the Empty Statement with Loop Statements


Occasionally, programmers make use of the empty statement when constructing a loop. The following example illustrates:


Because all the processing takes place within the loop update of the for statement, there is no need for a loop body. The loop condition i > j is initially true, causing the for statement to execute the empty statement (even though there is very little going on here) and the loop update repeatedly. As the loop update causes i to be decremented by one and j incremented by one, the loop condition will eventually be false when i is equal to j, which is half way between the two numbers (in this case 30 and 0), the result is thus 15. This programming practice does not produce clear code and should in most cases be avoided. It is mentioned here so you can recognize it when you stumble upon it in other programmers code.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: