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 (continued ...)

The Check It Button Event Code

The event for this button should assign the value entered by the user in the Guess textbox to an Integer variable. This value should then be compared to the randomly chosen value. If it is less than the random value the Text property of the feedback label should be changed to read Oops - too small. If it is greater the Text property of the feedback label should be changed to read Oops - too large, and if it is equal the Text property should read Hey - you guessed it!.

Itemising this more explicitly, the program statements are

  1. declare a variable (named guess) which will be assigned the value entered by the user
  2. assign the value of the Text property of the guess text box (named txtGuess) to the guess variable, using a conversion function to convert the string value
  3. if the guess value is less than the random value (variable name numToGuess)
  4. assign the string Oops - too small. Try again. to the Text property of the feedback label (named lblMessage)
  5. or if the guess value is greater than the random value
  6. assign the string Oops - too large. Try again. to the Text property of the feedback label
  7. otherwise (and the only case remaining is that the guess is equal to the random value)
  8. assign the string Hey - you got it! to the Text property of the feedback label
  9. end the if block statement

So add this event code to your project, and also program the Exit button event. Test the program and correct it if necessary.