If Statements.

 

If statements allow the program to have different outcomes depending on user input…..We will see this in our guessing game program..

 

 

Lets see if we can understand the code

 

Dim secretnumber As Integer

 

This code is for the Generate Secret number Button

 
Private Sub cmdsecret_Click()

'generates a secret number between 0 and 100

secretnumber = Int(Rnd(1) * 101 + 0)

End Sub

 

If you want to give up and cheat

 
 


Private Sub cmdgiveup_Click()

'reveals secret number

txtdisplay = secretnumber

End Sub

 

 

Private Sub txtguess_KeyPress(KeyAscii As Integer)

Checks what the user entered and then uses if statements to compare the guess with the actual secret number

 
' checks if answer is correct after enter key is hit.

Dim guess As Integer

 

If KeyAscii = 13 Then   'if i hit the enter key

  guess = txtguess.Text     'loads user guess into variable guess

  If guess > secretnumber Then lblmessage.Caption = "Too High"

  If guess < secretnumber Then lblmessage.Caption = "Too Low"

  If guess = secretnumber Then lblmessage.Caption = "You got it right"

End If

 

End Sub