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

## Presentation on theme: "BACS 287 Programming Fundamentals 2. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Statements – Expressions – Operators."— Presentation transcript:

BACS 287 Programming Fundamentals 2

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

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.

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

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.

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

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

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

BACS 287 Arithmetic Operators shoX = 1 + 2 + 3 + 4Result: 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)

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”

BACS 287 Comparison Operators >Greater than =Greater than or equal to <=Less than or equal to =Equal to <>Not equal to

BACS 287 Comparison Operators Evaluating an expression with comparison operators results in a value of True or False. blnA = 3 + 1 > 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)

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

BACS 287 -- Programming Fundamentals 214 Logical Operators AND 1 0 1 1 0 0 0 0 OR 1 0 1 1 1 0 1 0 Xor 1 0 1 0 1 0 1 0 Eqv 1 0 1 1 0 0 0 1 Imp 1 0 1 1 0 0 1 1 NOT Not True = False Not False = True

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

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

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)

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)

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

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

Similar presentations