280 likes | 415 Views
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
E N D
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 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
Display a quotation based on a user response. Example 1: Form mtbAnswer txtQuote
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
Example 1: Output Always gets executed! Life isn’t all fun and games.
Example 1: Output (continued) Y Life isn’t all fun and games.
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
Example 2: Form Given two numbers are not equal, which number is larger? txtFirstNum txtSecondNum txtResult
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
Evaluate whether a user knows the answer to this trivia question. Example 3: Form txtAnswer txtSolution
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
Nested If Blocks When one If block is contained inside another If block, the structure is referred to as nested If blocks.
Based on costs and revenues determine the profit or loss of a business. Example 4: Form
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
ElseIf Clause Ifcondition 1 Then action 1 ElseIfcondition 2 Then action 2 ElseIfcondition 3 Then action 3 Else action 4 EndIf
Example 5: Form Given two numbers (might be equal), which number is larger? txtFirstNum txtSecondNum txtResult
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
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
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
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
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
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.