200 likes | 352 Views
BACS 287. Programming Fundamentals 2. Programming Fundamentals. This lecture introduces the following topics: Statements Expressions Operators. Statements. A statement is a command to the computer telling it to do something.
E N D
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
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 Assignment Statements 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 + 4 Result: 10 shoY = 5 - 7 Result: -2 shoZ = 1 * 2 * 5 Result: 10 shoA = 2 ^ 4 Result: 16 sngA = 3 / 2 Result: 1.5 shoB = 7 \ 2 Result: 3 shoC = 7 Mod 4 Result: 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 & “ ” & strY Result: “Hi Mom” BACS 287
Comparison Operators > Greater than < Less 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 And Logical And AndAlso “short-circuit” Logical AND only after VB6 Or Logical Or OrElse “short-circuit” Logical OR only after VB6 Not Logical Not Xor Exclusive or Eqv Logical equivalence Not after VB6 Imp Logical implication Not after VB6 BACS 287
Logical Operators • AND • 1 0 • 1 1 0 • 0 0 0 • Xor • 1 0 • 1 0 1 • 0 1 0 • Imp • 1 0 • 1 1 0 • 0 1 1 • OR • 1 0 • 1 1 1 • 0 1 0 • Eqv • 1 0 • 1 1 0 • 0 0 1 NOT Not True = False Not False = True
Order of Precedence () Highest ^ - (negation) * / \ Mod + - & = <> < > <= >= Like, Is Not And Or Xor Eqv Imp Lowest
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 = Tostring(shoX) 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 BACS 287