murach, chapter 5. common control structures selection: the if statement case: the select case...

24
Control Structures Murach, Chapter 5

Upload: dylan-moore

Post on 17-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

Coding Boolean Expressions Relational operators – Compare two operands Logical operators 9/28/20083

TRANSCRIPT

Page 1: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

Control StructuresMurach, Chapter 5

Page 2: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

2

Common Control StructuresSelection: The IF statementCase: The Select Case statementIteration: Loops

9/28/2008

Page 3: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

3

Coding Boolean ExpressionsRelational operators – Compare two operandsLogical operators

9/28/2008

Page 4: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 5: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 6: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

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

Page 7: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

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

Page 8: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

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

Page 9: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

9

Operation Order of PrecedenceArithmetic operationsRelational operationsNOTANDOR

9/28/2008

Page 10: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

Conditional StatementsIf Select Case

Page 11: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 12: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 13: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 14: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 15: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 16: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 17: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 18: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

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

Page 19: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

19

LoopsForDo

9/30/2008

Page 20: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 21: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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

Page 22: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

© 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]

Page 23: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

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.

Page 24: Murach, Chapter 5. Common Control Structures Selection: The IF statement Case: The Select Case statement Iteration: Loops 9/28/20082

24

Test Loops

9/30/2008