Download presentation

Presentation is loading. Please wait.

Published byJanice Costello Modified about 1 year ago

1
BACS 287 Programming Fundamentals 2

2
BACS 287 Programming Fundamentals This lecture introduces the following topics: – Statements – Expressions – Operators

3
BACS 287 Statements A statement is a command to the computer telling it to do something. Within statements you can create expressions by combining operators, values, and procedures. An expression is a statement fragment made up of values, variables, and operators. Expressions return a single value.

4
BACS 287 Statement Types Assignment Statements Declaration Statements (Dim,Public,...) Object-oriented statements (Inherits,…) Program Control Statements (IF, Do While...) Procedure / Function calls Compiler Directives Input/Output statements (Writeline, Readline, FileOpen...)

5
BACS 287 Assignment Statements The simplest kind of statement is an assignment statement. Variable = Expression | Value A single variable is on the left and a value, simple expression, or compound expression is on the right. The right side is evaluated and the value is stored in the variable.

6
BACS 287 Assignment Statements Correct Examples: shoX = 10 intZ = intX strY = “VB7” intC = MAX_SCORE blnH = True strH = “” intR = 1 + shoX strG = “A” & “B” Incorrect Examples: 3 = 1 intX + 1 = 7 intY = = “string” vbTrue = False intX = “string” strY = 7 lngX = shoY + shoZ

7
BACS 287 Assignment Statements The expression on the right side of the assignment statement can be arbitrarily complex. These are made-up of: – Variables / Constants / Literals – Arithmetic operators – String operators – Comparison operators – Logical operators – In-place operators – Functions, Methods

8
BACS 287 Arithmetic Operators +Addition -Subtraction *Multiplication /Floating-point division \Integer division (truncates remainder) Mod Modulus (remainder division) ^Exponentiation

9
BACS 287 Arithmetic Operators shoX = Result: 10 shoY = 5 - 7Result: -2 shoZ = 1 * 2 * 5Result: 10 shoA = 2 ^ 4Result: 16 sngA = 3 / 2Result: 1.5 intB = 7 \ 2Result: 3 intC = 7 Mod 4Result: 3 (1 with remainder of 3)

10
BACS 287 String Operators Most of the operations that you can do to strings use built-in functions. There is only 1 string manipulation operator. &string concatenation strX = “Hi” strY = “Mom” strZ = strX & “ ” & strYResult: “Hi Mom”

11
BACS 287 Comparison Operators >Greater than

12
BACS 287 Comparison Operators Evaluating an expression with comparison operators results in a value of True or False. blnA = > 3 True blnB = 2.3 >= 1.1 * 2 True blnC = “abc” <> “a”&“b”&“c” False blnD = False = True False blnE = 0 = (2 < 1) True (2 < 1 is false, which equals 0)

13
BACS 287 Logical Operators AndLogical And AndAlso“short-circuit” Logical AND only after VB6 OrLogical Or OrElse“short-circuit” Logical OR only after VB6 NotLogical Not XorExclusive or EqvLogical equivalence Not after VB6 ImpLogical implication Not after VB6

14
BACS Programming Fundamentals 214 Logical Operators AND OR Xor Eqv Imp NOT Not True = False Not False = True

15
Order of Precedence ()Highest ^ -(negation) * / \ Mod + - & = <> = Like, Is Not And Or Xor Eqv Imp Lowest

16
BACS 287 In-Place Operators In-place operators are short-cuts to common operations. Cnt += 1 same as Cnt = Cnt + 1 Cnt -= 1 same as Cnt = Cnt - 1 Sum *= 2 same as Sum = Sum * 2 Avg /= 3 same as Avg = Avg / 3 Tot \= 3 same as Tot = Tot \ 3

17
BACS 287 Functions The right side of an assignment statement can use functions. These can be built-in or user-defined. Examples: strX = val(shoX) strString = Ucase(strInput) datFuture = DateAdd(‘d’, 21, #1/31/2003#) intY = myFunction(shoX, shoY)

18
BACS 287 Methods An alternative way to perform built-in tasks is to use methods. Many of the built-in functions have equivalent methods in VB.Net. Examples: strX = sho.ToString strString = strInput.Ucase datFuture = datNow.Adddays(21)

19
BACS 287 Expression Examples decWorth = decAssets - decLiabilities datStartTime = Now datEndDate = Now + 17 sngCubeRoot = intX ^ (1/3) shoX = 1 + ((2 + 3) * 2) * 2 intX = 8125 \ 1000 shoCnt += 1 intLength = Len(strTest) If intTemp >= 451 And Flammable = vbTrue... strValue = “abc” & “1” & strXYZ blnX = intCount * 2 < 15 Or strColor = “Blue” And intCount * 2 < 21

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google