Page 138 - Open Soource Technologies 304.indd
P. 138

Event Driven Programming




                                        Rerun the program, type “Y” into the masked text box, press the button, and
                                        observe that the description of the game is skipped.




                          6.2.2 ElseIf Clauses
                          An extension of the If block allows for more than two possible alternatives with the inclusion
                          of ElseIf clauses. A typical block of this type is

                          If condition1 Then
                            action1
                          ElseIf condition2 Then
                            action2
                          ElseIf condition3 Then
                            action3
                          Else
                            action4
                          End If
                          This block searches for the first true condition, carries out its action, and then skips to the
                          statement following End If. If none of the conditions is true, then Else’s action is carried out.
                          Execution then continues with the statement following the block. In general, an If block can
                          contain any number of elseIf clauses. As before, the Else clause is optional.

                                        5: The following program redoes Example 5 so that if the two numbers are
                                           equal, the program so reports:















                          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 = “The larger number is “ & num1
                            ElseIf (num2 > num1) Then
                               txtResult.Text = “The larger number is “ & num2
                           Else
                               txtResult.Text = “The two numbers are equal.”



                          132                    LOVELY PROFESSIONAL UNIVERSITY
   133   134   135   136   137   138   139   140   141   142   143