Selection Part 2 Using Logical Operators, how strings are compared and random numbers.

Slides:



Advertisements
Similar presentations
Strings Testing for equality with strings.
Advertisements

Selection Structures Tonga Institute of Higher Education.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
New Mexico Computer Science For All Booleans and Logic Maureen Psaila-Dombrowski.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Fundamentals of Python: From First Programs Through Data Structures
Chapter 8: String Manipulation
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
An Introduction to Textual Programming
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 4: The Selection Structure
A453 Exemplar Password Program using VBA
Tutorial 11 Using and Writing Visual Basic for Applications Code
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Chapter 4 The If…Then Statement
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic Games: Prepare for Hangman
Shorter of two objects and changing color V2 Functions, events and setting the color in sequence and randomly This is a modification of the Changing Color.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana NWLAPCUG - May 15th 2008.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
30/10/ Iteration Loops Do While (condition is true) … Loop.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Scratch Programming Lesson 5 Programming logic. We are going to learn… Initialize the variables SET VS CHANGE Operator (Part II) Use a variable as a counter.
Chapter 5: More on the Selection Structure
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Decision Structures, String Comparison, Nested Structures
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Random Numbers Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
A variable is a name for a value stored in memory.
Chapter 4 The If…Then Statement
CHAPTER FIVE Decision Structures.
Chapter 4: The Selection Structure
Decision Making in Code Logical Tests & Truth Logical Expressions
Event Driven Programming
Visual Basic..
Department Array in Visual Basic
Chapter (3) - Looping Questions.
Introduction to TouchDevelop
Visual Basic: Week 5 Review User defined functions
Write a program that places the names of five different states in labels, and allows the user to enter guesses of the corresponding state capitals in textboxes.
Variables & getting info from the user
The Data Element.
The Data Element.
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Presentation transcript:

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 TrueANDTrue ANDFalse ANDFalse TrueORTrue ORFalseTrue FalseORFalse NOTFalseTrue NOTTrueFalse

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 Example Conditions

Practical Example Dim mark as single Mark=txtMark.text If mark >=70 and mark <80 then messagebox.show(“You have a B!”) End if

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 Practical Example #2

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 and

RND() con’t…. By generating a random number between and 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 to to 2 -3 to 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.