Date post: | 17-Jan-2018 |
Category: |
Documents |
Upload: | dylan-moore |
View: | 219 times |
Download: | 0 times |
Control StructuresMurach, Chapter 5
2
Common Control StructuresSelection: The IF statementCase: The Select Case statementIteration: Loops
9/28/2008
3
Coding Boolean ExpressionsRelational operators – Compare two operandsLogical operators
9/28/2008
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 4
Relational operators Operator Name = Equal to <> Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Is Is IsNot IsNot
© 2008, Mike Murach & Associates, Inc.Modified Murach’s Visual Basic 2008, C5 Slide 5
Code that uses relational operators isValid = False ' equal to the False value txtYears.Text = “” ‘ equal to an empty string message Is Nothing ' equal to a null value address IsNot Nothing ' not equal to a null value
6
Comparing different numeric value typesVB casts less precise type to the more precise
typeExample: decVar < intVar The integer value is cast to a decimal value.
9/28/2008
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 7
Logical operators Name Description And Returns a True value if both expressions are True. This
operator always evaluates both expressions. Or Returns a True value if either expression is True. This
operator always evaluates both expressions. AndAlso Returns a True value if both expressions are True. This
operator only evaluates the second expression if necessary.
OrElse Returns a True value if either expression is True. This operator only evaluates the second expression if necessary.
Not Reverses the value of the expression.
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 8
Code that uses logical operators subtotal >= 250 AndAlso subtotal < 500 timeInService <= 4 OrElse timeInService >= 12 Not (counter + 1 >= years)
9
Operation Order of PrecedenceArithmetic operationsRelational operationsNOTANDOR
9/28/2008
Conditional StatementsIf Select Case
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 11
The syntax of the If statement If condition Then statements [ElseIf condition Then statements] ... [Else statements] End If
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 12
An If statement without an ElseIf or Else clause
If subtotal >= 100 Then discountPercent = .2D End If
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 13
An If statement with an Else clause
If subtotal >= 100 Then discountPercent = .2D Else discountPercent = .1D End If
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 14
An If statement with ElseIf clauses If subtotal >= 100 And subtotal < 200 Then discountPercent = .2D ElseIf subtotal >= 200 And subtotal < 300 Then discountPercent = .3D ElseIf subtotal >= 300 Then discountPercent = .4D Else discountPercent = .1D EndIF
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 15
Nested If statements I f customerType = "R" Then If subtotal >= 100 Then discountPercent = .2D Else discountPercent = .1D End If Else discountPercent = .4D EndIf
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 16
The syntax of the Select Case statement Select Case testexpression [Case expressionlist statements] ... [Case Else statements] End Select
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 17
A Select Case statement based on customer type codes
Select Case customerType Case "R" discountPercent = .1D Case "C" discountPercent = .2D Case Else discountPercent = 0 End Select
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 18
A Select Case statement based on order quantities Select Case orderQuantity Case 1, 2 discountPercent = 0D Case 3 To 9 discountPercent = .1D Case 10 To 24 discountPercent = .2D Case Is >= 25 discountPercent = .3D Case Else discountPercent = 0 End Select
Keywords that you can use in the expression list Keyword Meaning To Specifies a range of values. Is Precedes a conditional expression.
19
LoopsForDo
9/30/2008
© 2008, Mike Murach & Associates, Inc.9/30/2008 Slide 20
The syntax of the For…Next statement For counter [As datatype] = start To end [Step step] Statements Next [counter]
A For loop that adds the numbers 0 through 4 Dim sum As Integer Dim i As Integer For i = 0 To 4 sum += i Next
© 2008, Mike Murach & Associates, Inc.9/30/2008 Slide 21
The syntax of the For…Next statement For counter [As datatype] = start To end [Step step] Statements Next [counter]
A For loop that adds the numbers 2, 4, 6, 8, 10, and 12
Dim sum As Integer For i As Integer = 2 To 12 Step 2 sum += i Next
© 2008, Mike Murach & Associates, Inc.Murach’s Visual Basic 2008, C5 Slide 22
The syntax of the Do loop with the test first Do [{While|Until} condition] statements Loop
The syntax of the Do loop with the test last Do statements
Loop [{While|Until} condition]
Do NOT use EXIT or CONTINUEStructure the logic so that their use is
unnecessary.If you use either statement in a programming
assignment and I can write the code without using them, you will receive a zero for your grade.
24
Test Loops
9/30/2008