Chapter 3- Data Types and Operators Boolean Expressions Page 2 3 4

Introduction

The If Statement Page 2 3 4 5 6 7 8 9
Arithmetic Operations Page 2 3 4 5 6 Boolean Operators and Nested If Statements Page 2 3 4 5 6 7
Events and Sequential Processing Page 2 3 4 5 More Examples Page 2 3 4 5 6 7 8 9 10 11 12
Datatypes and Conversions Page 2 3 4 5 6 7 Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10
Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Exercises Page 2 3 4 5 6 7 8
Chapter 4- Selection Statements Review Questions
Introduction

4-3: If Statements

Exercise 4-2: Number Guessing Game

Figure 4-2: The Guessing game interface

In this exercise you'll design a simple game in which the player tries to guess a number that has been selected randomly between 1 and 100. The interface is shown in the figure.

When the user enters a guess and presses the Check It button a message is displayed giving feedback about the guess. Feedback consists of a message such as too small, too large, or equal to the number. This is shown in the second figure. The user can then enter a new guess in the Guess textbox and press the Check It button again.

Figure 4-3: The Guessing game interface

When the user presses the Play Again button (presumably after they have successfully guessed the current number) a new random number is generated, the Guess textbox is cleared and the feedback message is reset to the string shown in the first figure.

The feedback message is a label object, which has its Text property set to the appropriate message string depending on the guess entered by the user. Thus the program involves making a decision about what to assign to the Text property based on comparing the guess and the random value.