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.
Published byModified over 4 years ago
Presentation on theme: "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."— Presentation transcript:
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 in MaxPrice) Even though an equal sign (=) means replacement in an assignment statement, in a relation test (using relational operators) an equal sign is used to test for equality
Comparing Strings String variables can be compared to other string variables or ‘string literals’ enclosed in quotation marks The comparison begins with the Left-Most character and proceeds one character at a time from left to right Each character is compared in the strings, and as soon as one is less than another the comparison is terminated and the string with the lower ranking character is judged less than the other The determination of which character is less than other is based on the code used to store characters internally in the computer
ASCII code (American Standard Code for Information Interchange) has an established order called the collating sequence, for all letters, numbers, and special characters All numeric digits are less than all letters Comparing the Text Property of Text Boxes The Text Property can behave like a Variant, a String, a Number A numeric expression/calculation on a Text Property can be forced by using the Val function
Testing for Boolean (True/False) If X = True Then…. is equivalent to: If X Then…. Boolean Variables hold the value ‘0’ when False, and ‘-1’ when True Any variable can be tested for ‘True’/‘False’ VB considers any numeric variable with a value of ‘0’ to be False, any other value will evaluate True The variable or expression is referred to as an ‘implied condition’ iCounter = 10 If iCounter = 10 Then... If iCounter Then...
Comparing Uppercase and Lowercase Characters When comparing strings, the case of the characters is important An Uppercase character is not equal to a Lowercase character By using the String Function Ucase or Lcase, the Uppercase or Lowercase equivalent of a string can be compared Ucase(string) Lcase(string)
Text1.Text Ucase(Text1.Text) Lcase(Text1.Text) User types in Basic BASIC basic If Ucase(Text1.Text) = “BASIC” Then Print “Hello” End If When the text of Text1 is converted to Uppercase, it must be compared to an Uppercase ‘Literal’ (BASIC), if it is to evaluate True
Select Case Statements (Case Structure) A Select Case statement executes one of several groups of statements, depending on the value of an expression An expression is selected and tested against a number of possible cases The expression to be tested is usually a variable or object property There is no limit to the number of statements that can follow a Case statement If any of these cases match the value of the expression, the statements that follow are executed
Select Case statement syntax: Select Case [Expression] Case [Expression Value/Constant List] Statements Case [Expression Value] Statements Case [Expression Value] Statements Case Else Statements End Select
The [Expression Value/Constant List] is the value that is required to be matched, and can be of the following types: numeric or string type variable or constant a range of values a relational condition or a combination of these To test a single variable or expression for multiple values, the Case Structure provides a flexible and powerful solution Any program decisions that can be coded with a Case Structure can also be coded with Nested If Statements, but usually the Case structure is simpler and clearer
When using a Relational Operator the word Is must be used Case Is >= 100 To indicate a Range of Constants, use the word To Case 25 To 90 To test for a String Value, quotation marks must be included around the literals Case “Visual Basic” In using Strings, the case(Upper/Lower) of the characters must be exact between the expression being tested and the [Expression Value/Constant List] Select Case Ucase(Text1.Text) Case “VISUAL BASIC” A combination of Relational/Logical Operators, using Variables and Constants Case Is >= 10 AND Num1 <= 20
Although the Case Else clause is optional, generally it will be included in the Select Case statements The statements coded underneath Case Else will execute only if none of the other Case Conditions is matched The Case Else clause will provide checking for any invalid or unforeseen values of the expression being tested If the Case Else clause is omitted and none of the Case Conditions is True, the program will continue execution at the statement following the End Select If more than one Case value is matched by the expression, only the statements in the first Case clause will be executed, identifying a definite Hierarchy