350 likes | 359 Views
Learn about mathematical operations, comparison operators, special operators, conversion operations, and condition statements in Visual Basic.
E N D
CSI 101 Elements of ComputingSpring 2009 Lecture #7 – Visual Basic Operations and Conditions Monday, February 23rd, 2009 thru Wednesday, February 25th, 2009
Visual Basic Operations How to get things done
Types of Operations • Mathematical Operators • Symbols that represent various mathematical operations • Keywords • Reserved words in the Visual Basic language • Perform specific actions • Methods • A specific function attached to an object
Mathematical Operations • Many calculations in Visual Basic are mathematical • Using a value contained in a variable, altering it somehow, and finishing with a result • Big difference between math and Visual Basic • The answer, or receiving variable, is ALWAYS to the left side of the equal sign • Example: intCount = intCount + 1 This adds one to the value of variable intCount
Arithmetic Operators • + • - • * • / • \ • Mod • ^ • Note there is no symbol for roots • Addition • Subtraction • Multiplication • Division • Integer Division (has no decimal/remainder) • Modular division (returns remainder only) • Exponentiation
Arithmetic Examples GPA = sumGrades / totHours dblAve = (int1 + int2 + int3) / 3 Sqr = intVal ^ 2 intDiff = A – B intRank = Total \ num 5 /4 is 1.25 5 \ 4 is 1 6 MOD 4 is 2
Order of Operations • Uses same order as math: • Parentheses (inside out) • Exponentiation • Multiplication or division (left to right) • This includes integer division and modular division • Addition and subtraction (left to right)
Comparison Operators • < • > • = • <= • >= • <> • AND • NOT • OR • XOR • Less than • Greater than • Equal to • Less than or equal to • Greater than or equal to • Not equal to • Joins two conditions: True if BOTH conditions are true • Negates current condition result • Joins two conditions True if AT LEAST ONE of the conditions are true • Joins two conditions: True if ONLY ONE of them is True
Comparison Examples intCount < 10 intSum > 200 AND intSum < 400 (Note that 200 < intSum < 400 is NOT known to Visual Basic) boolCheck = True OR Length < 10
Special Operators • & • += • -= • *= • /= • &= • Concatenate strings • Increment • Decrement • Multiplication assignment • Division assignment • Add string to end
Special Operator Examples Start with intSample = 1 intSample += 2 sets intSample to 3 intSample -= 1 sets intSample to 2 intSample *= 3 sets intSample to 6 intSample /= 2 sets intSample to 3
Concatenation Examples txtA = “Test A”, txtB =“BB”, txtC = “C” txtC = txtA & txtB leaves A and B unchanged, txtC now is “TestABB” txtA &= txtB leaves B unchanged, txtA now has “Test ABB” txtC = txtA & “ “ & txtC leaves A unchanged, txtC now has “TestABB TestABB”
Conversion Operations How to change things
Why use conversion? • Sometimes you have data of one data type (like string) and need to use it in a calculation as another type (like integer) • Does not alter the variable, since it was declared as a particular type • Allows the program to copy that value to a new variable of the desired data type
Convert from String • Use the Parse method of a data type • For instance, to change to Integer, use the Integer.Parse( ) method • intValue = Integer.Parse(strValue) • The following data types have Parse methods: • Integer, Short, Long • Decimal, Single, Double • The following data types do not have Parse methods: • Boolean • Date
Convert To String • All numeric types have a ToString method • txtTest = intCount.ToString( ) • Boolean variables do NOT have a ToString method
Formatting Options You can specify how you want the converted text value to look Specify option within parentheses of ToString method
Numeric Formats • Currency • Fixed • Number • Digits • Percent • Two decimal places, dollar sign preceding value, negative values enclosed in parentheses • Variable decimal places, no comma separators, negative sign preceding negative values • Fixed with comma separators • Number with no decimals • Multiplies value by 100, adds percent sign to end
Numeric Format Options • C or c • Fn or fn • Nn or nn • Dn or dn • Pn or pn • Currency • Fixed with n decimal places • Example: F3 has 3 decimal places • If no digit specified, uses two decimal places • Number with n decimal places • Digits of n digits long • Percent with n decimal places
Numeric Format Examples intA = 12 and decA = 6.45 decA.ToString(“C”) yields $6.45 decA.ToString(“F3”) yields 6.450 decA.ToString(“N”) yields 6.45 decA.ToString(“N1”) yields 6.5 intA.ToString(“D3”) yields 012 intA.ToString(“P”) yields 12.00 % intA.ToString(“P0”) yields 12 %
Condition StatementsIF How to decide what to do
Decision statements • Select one action or another • Similar to the diamond-shaped box in flowcharts • Either True or False (Yes or No) • Condition evaluates to either True or False • Uses comparison operators • IF block contains condition(s)
IF statement forms • Single-line: • IF condition THEN action [ELSE action] • If condition is True, then perform action following the THEN keyword • The ELSE part is optional • If condition is False, action following ELSE is performed • Note that only one operation can follow the THEN or ELSE • Example: IF intCount < 10 THEN intCount += 1
IF statement forms, cont • Multi-line: • Used if more than one operation is performed if condition is True or False • IF condition THEN <actions> ELSE <actions> END IF
ELSEIF statement • Provides a different condition • Performed if condition on IF was False • Inserted before ELSE • Now ELSE is run if BOTH conditions are False
ELSEIF format IF condition THEN <actions> ELSEIF new_condition THEN <actions> ELSE <actions> END IF
IF Example IF intCount < 10 THEN intSum += intNums(index) index += 1 intCount += 1 ELSE dblAve = intSum / 10 END IF
ElseIf Example IF index< limit THEN intSum += intNums(index) index += 1 intCount += 1 ELSEIF dblAve = 0 THEN dblAve = intSum / intCount ELSE intCount += 1 END IF
Nested IF A multi-line IF block contained within another multi-line IF block Used to process actions dependent upon two independent conditions
Nested IF Example Find smallest of three values: IF A < B Then IF A < C Then Min = A Else Min = C ELSE IF B < C Then Min = B Else Min = C END IF
Condition StatementsSELECT How to have lots to choose from
Select Case Statement • Allows for multiple conditions • Conditions are values, not comparisons that result in True or False • Each condition can have multiple values • Will perform actions for first case that is found true • Never runs more than one, so whatever one is first wins
Select Case Format Select Case expression Case values <actions> Case values <actions> : [Case Else] [<actions>] End Select
Case Value lists • Single value • Case 2 • List of values • Case 4,6,8 • Bounded values • Case < 7 • Range of values • Case 5 TO 10
Select Case Example Select Case intValue Case < 5 intValue += index index += 1 Case < 10 intValue += 2 Case < 20 intValue /= 2 End Select