Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Selection Part 2 Using Logical Operators, how strings are compared and random numbers Logical Operators • Logical operators are used to create more complex conditions • They include AND, OR and NOT True AND True True True AND False False False AND False False True OR True True True OR False True False OR False False NOT False True NOT True False Example Conditions 1=1 and 1=2 is false 2=3 or 2=2 is true Not 1=2 is true 1<>1 and 4<9 is false 9=10 or 5>9 is false 4.6 >4 or 4.6>5 is true Not true is false Practical Example Dim mark as single Mark=txtMark.text If mark >=70 and mark <80 then messagebox.show(“You have a B!”) End if Practical Example #2 Dim number as integer Messagebox.show(“I have 2 numbers between 1 and 10. Can you guess one of them?”) Number=txtGuess.text If number = 3 or number=8 then messagebox.show(“Correct”) Else messagebox.show(“Wrong! Guess again!”) End if Comparing Strings • Every character has a different ascii number associated with it • Example: a 97, b 98, c 99 etc. A 65, B 66, C 67 – Notice that capitals have a lower ascii value than lowercase letters – As result “Apple” is less than “apple” Comparing Strings con’t. “dog” = “dog” is true “cat = “ Cat” is false “elephant” < “Zebra” is false “abcd” > “Abcd” “abc” < “abcd” Practical Example Dim user, password as string User=txtUser.text Password=txtPassword.text If user=“krnic” and password=“computers” then Messagebox.show(“User validated. Key Unlocked!!!!”) End if Practical Example #2 Dim person as string Person=txtName.text Messagebox.show(“Please enter your name is lowercase letters.”) If person >=“m” then Messagebox.show(“You belong in the second half of the list”) Else Messagebox.show(“You belong in the first half of the list.”) End if Randomizing Numbers • Random numbers are often used to mimic behaviours in computer programs • For example, consider playing against a computer in a game of cards • By randomizing a number between 1 and 4 you could get the computer to act as if it is deciding which of its four cards it will give up Functions • A function generates/returns a value • To create a random number we can use a pre-built function called rnd() • To use this we must first use the randomize statement in the event where we want to use the rnd() function • The rnd() function will generate a random number between 0.0000 and 0.9999 RND() con’t…. • By generating a random number between 0.0000 and 0.9999 we can use some math to make a random number generator that includes any range of numbers • Here’s the formula: Math.round(min + rnd()* range) For example: if we want a random number from 1 to 4 our code would look as follows: Math.round(1+rnd()*3) Of course we should first create a variable and assign to it our number Dim number as integer Number=Math.round(1+rnd()*3) Practice What is the line of code that would generate the following random range of numbers: 1 to 10 20 to 30 -5 to 2 -3 to 10 100 to 1000 Practical Example randomize Dim number, playerGuess as integer Number=math.round(1 +rnd()*99) lblPrompt.text=“I have a number between 1 and 100. Can you guess it??” playerGuess=txtGuess.text If payerGuess=Number then messagebox.show(“Correct!”) Else messagebox.show(“Sorry but your guess was incorrect!”) End if Exercises 1. Create a program that allows a user to click a button that then places in a textbox a random number from 1 to 10. Exercise 2 2. Create a program that allows a user to guess a number selected randomly by the computer. The number is to be between 1 and 100. Make sure you assign the computer’s random number to a variable in the load event of the form otherwise each time the player presses the button to check the number a new one will be generated. Exercise #3 • 3. Create a program that allows a user to randomly relocate a red label on a form. Relocation occurs upon the click of a button. In order to do this you will need to determine the width and height of the form and then randomize numbers in these ranges i.e. 1 to formWidth and 1 to formHeight. You can change the location of the label by assigning new values to its left and top properties.