Presentation is loading. Please wait.

Presentation is loading. Please wait.

PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008.

Similar presentations


Presentation on theme: "PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008."— Presentation transcript:

1 PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008

2 2 © Dr. Herbert G. Mayer Agenda Assignment Statement Arithmetic Expressions Boolean Expressions If Statement For Statement Do While Statement Do Statement Procedure Call Return Statement

3 3 © Dr. Herbert G. Mayer Assignment Statement Assignment Statement move a newly computed value to a variable object Consists of 3 parts: –left-hand-side (lhs) assignable (variable) object –the = operator –and right-hand-side (rhs) expression, newly computed The expression on the rhs is executed first Then the result is moved to variable object on the lhs lhs and rhs can be subscripted array element e.g. Min_val( 17 + k ) = i * ( current(j) – 12 ) The types of the rhs and lhs must be compatible

4 4 © Dr. Herbert G. Mayer Assignment Statement Samples Variable= Expression‘ generic VB Num= TextBox1.Text‘ input from text box Index= Index + 1‘ increment Index += 1‘ shorter increment area= PI * r ^ 2‘ PI is constant 3.14159 circum= 2 * PI * r‘ circumference a(i)= a(i) + b(i-1) – c(i+1) * j ‘ same as: a(i) += b(i-1) – c(i+1) * j

5 5 © Dr. Herbert G. Mayer Arithmetic Expression Element of an arithmetic expression can be any of: –Literal –Constant, which is a symbolic literal –Object reference (variable use), including indexed array element –Function call, possibly with parameters –( ) with another, simpler expression inside Any arithmetic expression has a type Non-arithmetic types, such as String, also included in VB Two elements can be combined with dyadic operators, e.g. –+ - with low precedence –/ * Mod higher precedence –^ highest precedence Also & operator to concatenate string expressions Arithmetic element can be modified by monadic operators + - Operator precedence can be controlled via explicit parentheses, e.g. ( 3 * ( a + f() ) )

6 6 © Dr. Herbert G. Mayer Boolean Expression Element of Boolean expression can be a literal, a constant, an object reference, or relational operation: –Predefined Boolean Literals: True, False –Relational Expression: ( a_val > b_val ) Boolean expression can be used as a condition in If Statement, While Statement, etc. Two Boolean elements can be combined with dyadic Boolean operators, e.g. and, or, xor Examples: ( i < Max ) ( j <> 0 ) and ( ( m / j ) > 1 ) ( a = 98 ) and ( 2*k = m )

7 7 © Dr. Herbert G. Mayer If Statement If Statement with only an else clause selects 1 out of 2 options If Statement without else clause selects 0 or 1 ElseIf Clause can be used any number of times, including 0 Each ElseIf Clause needs condition when used If Statement is closed with End If keywords If the 1 st condition is true, associated statement list is executed, and If Statement completes Conditions evaluated in sequential, linear order

8 8 © Dr. Herbert G. Mayer If Statement Samples ‘ no Else-Clause If a > 12 Then j += 1‘ increment End If ‘ with Else- and with Elseif-Clause IF a > 1000 Then c = d * e ElseIf a > 100 Then c = d - e Else ‘ a <= 100 c = d End If

9 9 © Dr. Herbert G. Mayer If Statement Samples If b <> 0 Then‘ <> for Not Equal If a / b > 0.5 Then‘ note precedence increment( i )‘ call sub Else decrement( i )‘ call another sub End If Else MsgBox( “Zero Divide” )‘ output End If

10 10 © Dr. Herbert G. Mayer If Statement Simulates Logical And If ( a d ) Then foo() End If ‘ same as: If ( a < b ) Then If ( c > d ) Then foo() End If

11 11 © Dr. Herbert G. Mayer If Statement Simulates Logical Or If ( e = f ) Or ( g <> h ) Then bar() End If ‘ same as: If e = f Then bar() ElseIf g <> h Then bar() End If

12 12 © Dr. Herbert G. Mayer For Statement For Statement iterates through progression of values of the iteration variable through loop body in steps of 1 For Statement with Step Clause progresses through iterations in steps as defined by Step Clause, may be different from 1 Number of steps/iterations is computable before For Statement begins to run By default increment in steps of 1, counting upwards Iteration variable must be defined, and type compatible with low- and high-bounds --AKA start- and end-value With Step Clause progression of Steps can be different from 1 And may be counting downward, if step expression is negative Step Clause is optional For Statement may be executed 0 times, e.g. For i = 5 To 4 ‘ initial value exceeds final MsgBox( “i = “ & i ) Next

13 13 © Dr. Herbert G. Mayer For Statement Samples ‘ initialization of Arrays For i = 0 to Max-1‘ count up by 1, if Max >= 1 tab1(i)= i‘ 1 st table assigned index tab2(i)= i^2‘ 2 nd table assigned square of i tab3(i)= i^3‘ 3 rd table assigned i**3 Next ‘ table1() and table2() have at least 101 elements! Count = 0 For i = 100 to 0 Step -10 ‘ count down: steps of 10 table1( i ) = table2( i ) + 1 Count += 1 Next

14 14 © Dr. Herbert G. Mayer Do While Statement While Statement executes body of loop, while the controlling condition is true When number of iterations of loop unknown, use While Statement, AKA Do While loop Number of iterations may be 0, if the condition is false initially Ends, when condition is finally false While Statement doesn’t necessarily have an iteration variable Body of While Statement needs to change objects so that condition eventually yields false

15 15 © Dr. Herbert G. Mayer Do While Statement Samples Do While i < Max a( i ) = TextBox1.text i += 1 Loop ‘Newton’s sqrt() approximation, example: sqrt(2) arg = 2.0 ‘ generally: arg is function parameter oldr = arg ‘ initialize old root to arg newr = 1.0 ‘ random start value for new root Do While( Math.Abs( newr – oldr ) > 0.0000001 ) oldr = newr newr = ( oldr + ( arg / oldr ) / 2.0 ) Loop MsgBox( “Root(“ & arg & “) = “, newr )

16 16 © Dr. Herbert G. Mayer Do Statement Do Statement executes body until condition is true, but iterates at least once When number of steps unknown, but body must be executed at least once, use Do Statement Executes body as long as until condition is false Body of Do Statement needs to change objects during execution, so until condition eventually yields true

17 17 © Dr. Herbert G. Mayer Do Statement Samples ‘ 1.) initialize table a() i = 0 Do a( i ) = foo( i ) i += 1 Loop Until i >= Max ‘ 2.) Quicksort Do Do While Indata(i) < middle i += 1 Loop Do While ( middle < Indata(j) j -= 1 Loop if i <= j swap( Indata(i), Indata(j) ) i += 1 j -= 1 End If Loop Until i <= j

18 18 © Dr. Herbert G. Mayer Procedure Call Procedure call n() transfers control to named sub The return control back to successor of call Procedure n() must be defined as sub in scope If n() has formal parameters, the call statement must provide a matching actual for each formal Formal Value Parameter ( ByVal ) is initialized at point of call, can be assigned in sub, but does not change value of actual after return Formal Reference Parameter ( ByRef ) may be initialized at place of call, must be assigned in callée, and will preserve last value assigned after return

19 19 © Dr. Herbert G. Mayer Call Statement Samples ‘ 1.) sub declaration Private Sub swap_if( ByRef a As Integer, ByRef b As Integer ) Dim Temp As Integer = a If a < b Then a = b b = temp End If End Sub ‘ 2.) in same scope as swap_if() Private Sub Button2_Click(... )... For index1 = 0 to Max - 2 For index2 = index1 + 1 To Max - 1 swap_if( data( index1 ), data( index2 ) ) ‘ by reference! Next

20 20 © Dr. Herbert G. Mayer Return Statement Return Statement transfers control back to the calling environment Return Statement inside Sub body has no argument; transfers back to place after the last call Return Statement inside Function body has one argument –Also transfers back to place after the last call –Argument must be type compatible with function return type –At least 1 needed inside function body


Download ppt "PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008."

Similar presentations


Ads by Google