Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 101 Elements of Computing Spring 2009

Similar presentations


Presentation on theme: "CSI 101 Elements of Computing Spring 2009"— Presentation transcript:

1 CSI 101 Elements of Computing Spring 2009
Lecture #7 – Visual Basic Operations and Conditions Monday, February 23rd, 2009 thru Wednesday, February 25th, 2009

2 Visual Basic Operations
How to get things done

3 Types of Operations Mathematical Operators Keywords Methods
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

4 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

5 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

6 Arithmetic Examples GPA = sumGrades / totHours dblAve = (int1 + int2 + int3) / 3 Sqr = intVal ^ 2 intDiff = A – B intRank = Total \ num 5 /4 is \ 4 is 1 6 MOD 4 is 2

7 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)

8 Comparison Operators 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 < > = <= >= <> AND NOT OR XOR

9 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

10 Special Operators & += -= *= /= &= Concatenate strings Increment
Decrement Multiplication assignment Division assignment Add string to end

11 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

12 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”

13 Conversion Operations
How to change things

14 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

15 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

16 Convert To String All numeric types have a ToString method
txtTest = intCount.ToString( ) Boolean variables do NOT have a ToString method

17 Formatting Options You can specify how you want the converted text value to look Specify option within parentheses of ToString method

18 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

19 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

20 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 % intA.ToString(“P0”) yields 12 % Note that it rounds up if necessary

21 Condition Statements IF
How to decide what to do

22 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)

23 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 Explain use of square brackets to denote optional

24 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 END IF

25 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

26 ELSEIF format IF condition THEN <actions> ELSEIF new_condition THEN ELSE END IF

27 IF Example IF intCount < 10 THEN intSum += intNums(index) index += 1 intCount += 1 ELSE dblAve = intSum / 10 END IF

28 ElseIf Example IF index< limit THEN intSum += intNums(index) index += 1 intCount += 1 ELSEIF dblAve = 0 THEN dblAve = intSum / intCount ELSE END IF

29 Nested IF A multi-line IF block contained within another multi-line IF block Used to process actions dependent upon two independent conditions

30 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

31 Condition Statements SELECT
How to have lots to choose from

32 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

33 Select Case Format Select Case expression Case values <actions> : [Case Else] [<actions>] End Select

34 Case Value lists Single value List of values Bounded values
Range of values Case 5 TO 10

35 Select Case Example Select Case intValue Case < 5 intValue += index index += 1 Case < 10 intValue += 2 Case < 20 intValue /= 2 End Select


Download ppt "CSI 101 Elements of Computing Spring 2009"

Similar presentations


Ads by Google