1 / 31

Chapter 5

Chapter 5. Decisions. Outline and Objective. Relational and Logical Operators If Blocks Select Case Blocks. Relational Operators. The execution of the block If is controlled by a Boolean Expression a.k.a Conditional Statement A Conditional Statement can be either True or False

hazel
Download Presentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Decisions Chapter 5 - Visual Basic Schneider

  2. Outline and Objective Relational and Logical Operators If Blocks Select Case Blocks Chapter 5 - Visual Basic Schneider

  3. Relational Operators • The execution of the block If is controlled by a Boolean Expression a.k.a Conditional Statement • A Conditional Statement can be either True or False • Conditional Statements are formed by using six Relational operators and three Logical operators Chapter 5 - Visual Basic Schneider

  4. Relational Operators Chapter 5 - Visual Basic Schneider

  5. Logical Operators • The result of a logical operator is also True or False • The three Logical Operators are: Not And Or Chapter 5 - Visual Basic Schneider

  6. Not • Not: Negates a single expression • Example: Suppose answ = “Y” Not ( answ = “y”) is True Chapter 5 - Visual Basic Schneider

  7. And • Combines two expressions; each expression must be True for the entire expression to be True. • Example: Suppose answ = “Y” • (answ = “Y”) And (answ = “y”) is False Chapter 5 - Visual Basic Schneider

  8. Or • Combines two expressions; either expression (or both expressions) must be True for the entire expression to be True. • Example: Suppose answ = “Y” • (answ = “Y”) Or (answ = “y”) is True Chapter 5 - Visual Basic Schneider

  9. Determine whether the following conditions are true or false? • (“Y” <> “X”) And (143.55 < 143.55) • ( 0 = 14) Or (6 ^ 2 - 3 <= 4 / 2 + 8) • Not ( 6 = 7) And ( 44 > 33) Chapter 5 - Visual Basic Schneider

  10. Control Structures • The Selection portion of programming! • Allows the programmer to alter the normal flow of statement execution. • Allows computer to seem intelligent • By choosing a certain path by way of a Decision Chapter 5 - Visual Basic Schneider

  11. Types of Decision Structures • The Block If Statement • Simple alternative if • Compound alternative if….then…else • If…then…elseif Select Case Statement Chapter 5 - Visual Basic Schneider

  12. Single Alternative Decision • An action is taken if the condition is true, otherwise the control goes to the next statement. Chapter 5 - Visual Basic Schneider

  13. Single Alternative Decision • Syntax If condition Then action(s) End If If condition is true, action(s) is executed;otherwise action(s) is skipped Chapter 5 - Visual Basic Schneider

  14. Example of single-alternative decision If (grade > = 90) Then picOutput.Print “Excellent Student” letterGrade = “A” picOutput.Print “Your grade is “; letterGrade End If Chapter 5 - Visual Basic Schneider

  15. Compound Alternative Decision • Syntax IfconditionThen action1 Else action 2 End If Chapter 5 - Visual Basic Schneider

  16. Example of Compound If statement(find the larger of two numbers) Private Sub cmdFindLarger_Click() Dim largerNum As Single picResult.Cls If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then largerNum = Val(txtFirstNum.Text) Else largerNum = Val(txtSecondNum.Text) End If picResult.Print "The larger number is"; largerNum End Sub Chapter 5 - Visual Basic Schneider

  17. Important Points in Using If Statement • Use indentation • In the nested If statement, each If must have its own End If statement • Care should be taken to make If blocks easy to understand Chapter 5 - Visual Basic Schneider

  18. If cond1 Then If cond2 Then action(s) End If End If If cond1 And cond2 Then action(s) End If Example This is easier to understand A confusing If Block Chapter 5 - Visual Basic Schneider

  19. Compound Alternative Decision • Syntax Ifcondition1Then action1 ElseIfcondition2Then action 2 ElseIfcondittion3 Then action3 Else action4 End If Chapter 5 - Visual Basic Schneider

  20. Example (find the larger of two numbers, and report if the two numbers are equal) Private Sub cmdFindLarger_Click() picResult.Cls If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then picResult.Print "The larger number is"; txtFirstNum.Text ElseIf Val(txtSecondNum.Text) > Val(txtFirstNum.Text) Then picResult.Print "The larger number is "; txtSecondNum.Text Else picResult.Print "The two numbers are equal." End If End Sub Chapter 5 - Visual Basic Schneider

  21. Example (find cost of phone call from NY to LA) Private Sub cmdDisplay_Click() Dim length As Single Call InputLength (length) Call DisplayCost(length) End Sub Chapter 5 - Visual Basic Schneider

  22. Sub DisplayCost & InputLength Private Sub DisplayCost (length As Single) ' Display the cost of a call PicOutput.Print "Cost of call: " ; FormatCurrency ( Cost (length)) End Sub Private Sub InputLength(length As Single) ' Request the length of a phone call length = Val (InputBox("Duration of the call in minutes? ")) End Sub Chapter 5 - Visual Basic Schneider

  23. Function Cost Private Function Cost (length As Single) As Single If length < 1 Then Cost = .46 Else Cost = .46 + (length –1) * .36 End If End Function Chapter 5 - Visual Basic Schneider

  24. The Select Case Block • Similar to If statement • Used instead of nested If statement • An action to be selected from a list of alternatives • Avoids confusion of deeply nested If blocks Chapter 5 - Visual Basic Schneider

  25. Select Case Block (Syntax) Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resort End Select Chapter 5 - Visual Basic Schneider

  26. Select Case Block Each value-list contains one or more of the following types of items separated by a comma • a constant • a variable • an expression • an inequality sign preceded by Is and followed by a constant, variable, or expression • a range expressed in the form a Tob , where a and b are constants, variables, or expressions. Chapter 5 - Visual Basic Schneider

  27. Example of Select Case letterGrade = txtGrade.text Select Case letterGrade Case “A”, “B” picOutput.Print “ Good Work” Case “C” picOutput.Print “Average Work” Case Else picOutputPrint “Poor Work” End Select Chapter 5 - Visual Basic Schneider

  28. Example of If statement letterGrade = txtGrade.Text If ( letterGrade = “A”) Or (letterGrade = “B”) Then picOutput.print “Good Work” ElseIf (letterGrade = “C”) Then picOutput.Print “ Average Work” Else picOutput.Print “Poor Work” End If Chapter 5 - Visual Basic Schneider

  29. Several different types of value-list • Private Sub cmdInterpret_Click() Dim x As Integer, y As Integer, num As Integer ' One, Two, Buckle My Shoe picPhrase.Cls x = 2 y = 3 num = Val(txtNumber.Text)Select Case numCase y - x, x picPhrase.Print "Buckle my shoe."Case Is <= 4 picPhrase.Print "Shut the door."Case x + y To x * y picPhrase.Print "Pick up sticks."Case 7, 8 picPhrase.Print "Lay them straight."Case Else picPhrase.Print "Start all over again."End SelectEnd Sub Chapter 5 - Visual Basic Schneider

  30. Selector Is a String Variable Private Sub cmdInterpret_Click() Dim firstName As String firstName = txtAnswer.Text Select CasefirstName Case "Thomas" picSolution.Print "Correct." Case "Woodrow" picSolution.Print "Sorry, his full name was" picSolution.Print "Thomas Woodrow Wilson." Case "President" picSolution.Print "Are you for real?" Case Else picSolution.Print "Nice try, but no cigar." End Select End Sub Chapter 5 - Visual Basic Schneider

  31. Summary Points • How to use If statements • Select Case statements • How to use relational operators • How to use Not, And, and Or in Boolean expressions Chapter 5 - Visual Basic Schneider

More Related