1 / 28

Chapter 4 – Decisions

Chapter 4 – Decisions. 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks 4.4 Input via User Selection. 4.2 If Blocks. If Block Nested If Blocks ElseIf Clauses Input Validation with If Blocks. Simple If Block Example. If condition Then action 1 End If

keziah
Download Presentation

Chapter 4 – Decisions

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 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks 4.4 Input via User Selection

  2. 4.2 If Blocks • If Block • Nested If Blocks • ElseIf Clauses • Input Validation with If Blocks

  3. Simple If Block Example Ifcondition Then action 1 EndIf Statement 2 Statement 3 Regardless of whether the condition in the If statement is true or false, these statements will be executed

  4. Display a quotation based on a user response. Example 1: Form mtbAnswer txtQuote

  5. Example 1 PrivateSub btnDisplay_Click(...) _ Handles btnDisplay.Click Dim msg AsString msg = "Skittles is an old form of bowling " & "in which a wooden disk is used to knock " & "down nine pins arranged in a square. " If txtAnswer.Text.ToUpper = "N" Then MessageBox.Show(msg, "") EndIf txtQuote.Text = "Life isn’t all fun and games.” EndSub

  6. Example 1: Output Always gets executed! Life isn’t all fun and games.

  7. Example 1: Output (continued) Y Life isn’t all fun and games.

  8. If Block The following program will take a course of action based on whether a condition is true. Ifcondition Then action 1 Else action 2 EndIf Will be executed if condition is true Will be executed if condition is false

  9. Pseudocode and Flowchart for an If Block

  10. Example 2: Form Given two numbers are not equal, which number is larger? txtFirstNum txtSecondNum txtResult

  11. Example 2: Code PrivateSub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum AsDouble num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 EndIf txtResult.Text = "Larger number: "& largerNum End Sub

  12. Example 2: Output

  13. Evaluate whether a user knows the answer to this trivia question. Example 3: Form txtAnswer txtSolution

  14. An answer between .5 and 1 inclusive will be considered correct. Example 3: Code PrivateSub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim answer AsDouble answer = CDbl(txtAnswer.Text) If (answer >= 0.5) And (answer <= 1) Then txtSolution.Text = "Good, " Else txtSolution.Text = "No, " EndIf txtSolution.Text &= "it holds about 3/4 gals." EndSub And

  15. Example 3: Output

  16. Nested If Blocks When one If block is contained inside another If block, the structure is referred to as nested If blocks.

  17. Based on costs and revenues determine the profit or loss of a business. Example 4: Form

  18. Example 4: Partial Code If costs = revenue Then txtResult.Text = "Break even" Else If costs < revenue Then profit = revenue - costs txtResult.Text = "Profit is " & _ FormatCurrency(profit) Else loss = costs - revenue txtResult.Text = "Loss is " & _ FormatCurrency(loss) End If End If

  19. Example 4: Output

  20. ElseIf Clause Ifcondition 1 Then action 1 ElseIfcondition 2 Then action 2 ElseIfcondition 3 Then action 3 Else action 4 EndIf

  21. Example 5: Form Given two numbers (might be equal), which number is larger? txtFirstNum txtSecondNum txtResult

  22. Example 5: Code Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is "& num2 Else txtResult.Text = "The two are equal." End If EndSub No “ElseIf" required after the last Else

  23. Example 6: Form This program assumes that the user will graduate (that is, has a GPA of 2 or more) and determines if the user will graduate with honors. txtGPA txtOutput

  24. Example 6: Partial Code Private Sub (…) Handles btnDetermine.Click Dim gpa As Double = CDbl(txtGPA.Text) Dim honors As String = "" If gpa >= 3.9 Then honors = " summa cum laude." ElseIf gpa >= 3.6 Then honors = " magna cum laude." ElseIf gpa >= 3.3 Then honors = " cum laude." ElseIf gpa >= 2 Then honors = "." End If txtOutput.Text = "You graduated" & honors End Sub

  25. Example 6: Output

  26. Input Validation The statement If (IsNumeric(txtBox.Text) = True) Then is commonly used to validate that input is numeric. It can be condensed to IfIsNumeric(txtBox.Text) Then

  27. Simplified Nested If Statement Care should be taken to make If blocks easy to understand. Ifcond1 Then Ifcond1 Andcond2 Then action Ifcond2 Then End If action End If End If Logical Nested

  28. Comment Some programs call for selecting among many possibilities. Although such tasks can be accomplished with complicated nested If blocks, the Select Case block (discussed in Section 4.3) is often a better alternative.

More Related