Else If clause If condition 1 Then action

  • Slides: 9
Download presentation
Else. If clause If condition 1 Then action 1 Else. If condition 2 Then

Else. If clause If condition 1 Then action 1 Else. If condition 2 Then action 2 Else. If condition 3 Then action 3 Else action 4 End If Chapter 5 1

Example: Form txt. First. Num txt. Second. Num txt. Result Chapter 5 2

Example: Form txt. First. Num txt. Second. Num txt. Result Chapter 5 2

Example: Code Private Sub btn. Find. Larger_Click(. . . ) _ Handles btn. Find.

Example: Code Private Sub btn. Find. Larger_Click(. . . ) _ Handles btn. Find. Larger. Click Dim num 1, num 2 As Double num 1 = CDbl(txt. First. Num. Text) num 2 = CDbl(txt. Second. Num. Text) If (num 1 > num 2) Then txt. Result. Text = "Larger number is " & num 1 Else. If (num 2 > num 1) Then txt. Result. Text = "Larger number is " & num 2 Else txt. Result. Text = "The two are equal. " End If End Sub Chapter 5 3

Lab Sheet 5. 5: Form Chapter 5 4

Lab Sheet 5. 5: Form Chapter 5 4

Lab Sheet 5. 5: Code Function Calculate. FICA(By. Val ytd. Earnings As Double, _

Lab Sheet 5. 5: Code Function Calculate. FICA(By. Val ytd. Earnings As Double, _ By. Val cur. Earnings As Double) As Double Dim social. Security. Ben. Tax, medicare. Tax As Double If (ytd. Earnings + cur. Earnings) <= 90000 Then social. Security. Ben. Tax = 0. 062 * cur. Earnings Else. If ytd. Earnings < 90000 Then social. Security. Ben. Tax = 0. 062 * (90000 - ytd. Earnings) End If medicare. Tax = 0. 0145 * cur. Earnings Return social. Security. Ben. Tax + medicare. Tax End Function Chapter 5 5

Lab Sheet 5. 5: Output Chapter 5 6

Lab Sheet 5. 5: Output Chapter 5 6

Comments • When one If block is contained inside another If block, the structure

Comments • When one If block is contained inside another If block, the structure is referred to as nested If blocks. • Care should be taken to make If blocks easy to understand. Chapter 5 7

Simplified Nested If Statement If cond 1 Then If cond 2 Then action End

Simplified Nested If Statement If cond 1 Then If cond 2 Then action End If If cond 1 And cond 2 Then action End If Less Confusing Nested If Chapter 5 8

More Comments • Some programs call for selecting among many possibilities. Although such tasks

More Comments • Some programs call for selecting among many possibilities. Although such tasks can be accomplished with complicated nested If blocks, the Select Case block is often a better alternative. Chapter 5 9