Presentation on theme: "The IfTest and Case Statement1 The IF Test A conditional statement that permits portions of code to be executed only if and when some special condition."— Presentation transcript:
The IfTest and Case Statement1 The IF Test A conditional statement that permits portions of code to be executed only if and when some special condition applies. (If there is some beer, get me a bottle, otherwise get me a cola.) (If it is raining, bring an umbrella)
The IfTest and Case Statement2 IF Test If (Condition T/F) Then ’ (do true stuff) Else ’ (do false stuff) End If
The IfTest and Case Statement3 IF Test (short form) If (Condition T/F) Then ’ (do true stuff) End If (use only if there is nothing to do in the Else branch)
The IfTest and Case Statement4 IF Test Example (1) Private Sub cmdDo_Click() If cmdDo.Caption = "Talk" Then lblBox.Caption = "ding ding" cmdDo.Caption = "clear" Else lblBox.Caption = "" cmdDo.Caption = "Talk" End If End Sub
The IfTest and Case Statement5 IF Test Example (2) >
The IfTest and Case Statement6 IF Test Example (3) Private Sub cmdBlink_Click() If lblBox.Visible = True then lblBox.Visible = False Else lblBox.Visible = True End If End Sub Since lblBox.Visible is boolean why not use If lblBox.Visible then
The IfTest and Case Statement7 The CASE Statement (1) This statement can be used instead of repeated “IF” tests. It is clearer and nearly self documenting. The following example is probably the best way to illustrate it. It allows a multi-way choice between routing something to the northwest, the midwest or to some unknown area. Many more branches are possible.
The IfTest and Case Statement8 The CASE Statement (2) Private Sub cmdWhere_Click() Select Case txtState.Text Case "WA", "OR" lblOffTo.Caption = "Northwest" Case "IL", "WI", "MN", "IA" lblOffTo.Caption = "Midwest" Case Else lblOffTo.Caption = ”unknown" End Select End Sub
The IfTest and Case Statement9 The CASE Statement (3)
Your consent to our cookies if you continue to use this website.