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

Event Driven Programming


                                             ByVal curEarnings As Double) As Double
                            ‘Calculate Social Security benefits tax and Medicare tax
                            ‘for a single pay period in 2005
                            Dim socialSecurityBenTax, medicareTax As Double
                            If (ytdEarnings + curEarnings) <= 90000 Then
                              socialSecurityBenTax = 0.062 * curEarnings
                            ElseIf ytdEarnings < 90000 Then
                              socialSecurityBenTax = 0.062 * (90000 - ytdEarnings)
                            End If
                            medicareTax = 0.0145 * curEarnings
                            Return socialSecurityBenTax + medicareTax
                          End Function
                          [Run, type 12345.67 and 543.21 into the top two text boxes, and press the button. The following
                          is displayed in txtTax.]
                          $41.56

                          Comments
                            1. Constructs in which an If block is contained inside another If block are referred to as nested
                               If blocks.
                            2. Some programs call for selecting among many possibilities. Although such tasks can be
                               accomplished with complicated If blocks, the Select Case block (discussed in the next
                               section) is often a better alternative.



                                        Write a program by using If-else block.



                          6.3 Do While Loops

                          A loop, one of the most important structures in Visual Basic, is used to repeat a sequence of
                          statements a number of times. At each repetition, or pass, the statements act upon variables
                          whose values are changing.
                          The Do loop repeats a sequence of statements either as long as or until a certain condition is true.
                          A Do statement precedes the sequence of statements, and a Loop statement follows the sequence
                          of statements. The condition, preceded by either the word “While” or the word “Until”, follows
                          the word “Do” or the word “Loop”. When Visual Basic executes a Do loop of the form
                          Do While condition
                            statement(s)
                          Loop
                          It first checks the truth value of condition. If condition is false, then the statements inside the
                          loop are not executed, and the program continues with the line after the Loop statement. If
                          condition is true, then the statements inside the loop are executed. When the statement Loop is




                          134                    LOVELY PROFESSIONAL UNIVERSITY
   135   136   137   138   139   140   141   142   143   144   145