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