1 / 49

CSI 1306

CSI 1306. PROGRAMMING IN VISUAL BASIC PART 3. Part 3. 1. Translating Loop/Repetition Instructions 2. Translation Set 3 3. Additional Material. 1. Translating Loop/Repetition Instructions. Repetition Control Statements.

osmond
Download Presentation

CSI 1306

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSI 1306 PROGRAMMING IN VISUAL BASIC PART 3

  2. Part 3 • 1. Translating Loop/Repetition Instructions • 2. Translation Set 3 • 3. Additional Material

  3. 1. Translating Loop/Repetition Instructions

  4. Repetition Control Statements • We use the same type of test (comparison) for loops as we use for conditional branch instructions • However, we are now testing to determine how often a block of code executes • Remember that a loop consists of 4 parts: • Set up where we initialize variables before entering the loop • A test to determine whether the instructions in the loop should be executed again • An instruction block which contains the instructions which are to be executed repeatedly • A change affecting the test condition so that we eventually exit the loop

  5. Repetition Control Statements • We are going to use three loop constructs, two of which have a variation • DO WHILE – LOOP DO – LOOP WHILE • DO UNTIL – LOOP DO – LOOP UNTIL • FOR - NEXT

  6. DO WHILE – LOOP DO WHILE (condition) Instruction block LOOP • Condition evaluates to True or False • If the condition is True, then the instructions in the instruction block are executed and the condition is evaluated again • The instructions in the instruction block must eventually change the condition to False • When the condition is False, the loop is exited

  7. DO – LOOP WHILE • The DO WHILE - LOOP construct tests the condition at the beginning of the loop • The DO - LOOP WHILE construct tests the condition at the end of the loop DO Instruction block LOOP WHILE (condition) • Because the test is at the end of the loop, the instructions in the instruction block will be executed at least once

  8. Line a b c 1.2.3 1 0 0 4 1 0 0 5 1 1 0 6 1 1 0 7 6 1 0 4 6 1 0 5 6 2 0 6 6 2 5 7 11 2 5 4 11 2 5 5 11 3 5 6 11 3 15 7 16 3 15 4 16 3 15 5 16 4 15 6 16 4 30 7 21 4 30 4 21 4 30 8 21 4 30 Example • If N = 17, what are the values of a, b, and c after this code is executed? • a = 1 (1) • b = 0 (2) • c = 0 (3) • Do While (a < N) (4) • b = b + 1 (5) • c = c + a - 1 (6) • a = a + 5 (7) • Loop (8)

  9. DO UNTIL – LOOP DO UNTIL (condition) Instruction block LOOP • If the condition is True, the loop is exited • The instructions in the instruction block must eventually change the condition to True • When the condition is False, the instructions in the instruction block are executed and the condition is evaluated again

  10. DO – LOOP UNTIL DO Instruction block LOOP UNTIL (condition) • This construct tests the condition at the end of the loop • Because the test is at the end of the loop, the instructions in the instruction block will be executed at least once

  11. If N = 3, what is the value of x after the following is executed? X = 0 (1) I = N (2) Do (3) X = X + I (4) I = I - 1 (5) Loop Until (I = 0) (6) ‘Done (7) Line X I 1,2 0 3 3 0 3 4 3 3 5 3 2 6 3 2 4 5 2 5 5 1 6 5 1 4 6 1 5 6 0 6 6 0 7 6 0 Example

  12. Repetition Control Statements

  13. FOR - NEXT • The FOR - NEXT loop is used to loop a specific number of times (there is no condition to test at either the beginning or end of the loop) FOR J = 1 TO 4 Instruction block NEXT J • We will execute the instruction block 4 times

  14. FOR - NEXT FOR counter = start TO end Instruction block NEXT counter • Counter is a variable that represents an integer • Start is the first integer value assigned to counter • End is the last integer value assigned to the counter • By default, each time through the loop, the counter is incremented by 1 • NEVER CHANGE THE VALUE OF THE COUNTER INSIDE THE LOOP

  15. FOR - NEXT • A variation is: FOR counter = start TO end STEP increment Instruction block NEXT counter • Increment can be a positive integer (count up) • Increment can be a negative integer (count down)

  16. FOR - NEXT • How many times would this loop execute? For J = 1 To 5 Step 5 Instruction block Next J • Once

  17. What will X and Y be? X = 4 (1) Y = 1 (2) For B = 1 to 3 (3) If (X>6) Then (4) X = X + 2 * Y (5) Else (6) X = X + Y (7) Y = Y + 1 (8) End If (9) Y = Y * 2 (10) Next B (11) ‘Done (12) Line X Y B 1,2 4 1 ? 3 4 1 1 4 4 1 1 6 4 1 1 7 5 1 1 8 5 2 1 9 5 2 1 10 5 4 1 11 5 4 1 3 5 4 2 4 5 4 2 6 5 4 2 7 9 4 2 8 9 5 2 9 9 5 2 10 9 10 2 11 9 10 2 3 9 10 3 4 9 10 3 5 29 10 3 9 29 10 3 10 29 20 3 11 29 20 3 12 29 20 3 Example

  18. If K = 0 initially, what will its value be after this code executes? For I = 1 To 3 (1) For J = 4 To I Step -1 (2) K = K + 1 (3) Next J (4) Next I (5) ‘Done (6) Line I J K 1 1 ? 0 2 1 4 0 3 1 4 1 2 1 3 1 3 1 3 2 2 1 2 2 3 1 2 3 2 1 1 3 3 1 1 4 1 2 1 4 2 2 4 4 3 2 4 5 2 2 3 5 3 2 3 6 2 2 2 6 3 2 2 7 1 3 2 7 2 3 4 7 3 3 4 8 2 3 3 8 3 3 3 9 6 3 3 9 Example

  19. Translating Loop Instructions • Look at the METHOD • Each loop can be translated using one of the loop constructs • If you know how many times you want to loop (ie. the start and end values), use the FOR – NEXT construct • Otherwise, use one of the other 4 constructs • Make your code easy to read. Use a uniform indentation scheme

  20. Example • Write the code to convert temperatures in ºC to ºF • The Celsius temperatures range from -10 degrees to +30 degrees in increments of 5 degrees • The conversion formula is F = 1.8C + 32 • Use a Do While loop

  21. Example Option Explicit 'Written By T. James Sub Convert1() Dim celsius as Integer 'Temp in C Dim fahren as Single 'Temp in F celsius = -10 Do While (celsius <= 30) fahren = 1.8 * celsius + 32 MsgBox (fahren & " F= " & celsius & " C") celsius = celsius + 5 Loop End Sub

  22. Example • Now, write the code using a FOR NEXT loop

  23. Example Option Explicit 'Written By T. James Sub Convert2() Dim celsius as Integer 'Temp in C Dim fahren as Single 'Temp in F For celsius = -10 to 30 step 5 fahren = 1.8 * celsius + 32 MsgBox (fahren & " F = " & celsius & " C") Next celsius End Sub

  24. Review of Comparisons • Sometimes, a simple test is not adequate for a conditional branch or loop instruction. We need a more complex test that combines the results of multiple simple tests • In this case, we can use the logical (Boolean) operators to combine test results • The logical operators, in order of precedence, are • NOT: changes a True to a False or a False to a True • AND: returns a True only when both conditions are True • OR: returns a True when either or both conditions are True • XOR: returns a True when only one of the conditions is True

  25. Review of Comparisons • Let X = 5, Y = 6 and Z = “WOMBAT” (X > 0) OR (Y >7) (TRUE) OR (FALSE) TRUE (X < 5) AND (Y > 0) (FALSE) AND (TRUE) FALSE (Y MOD 2 = 0) (6 MOD 2 = 0) (0 = 0) TRUE (Left(Z, 3) = “WOM”) (Left(“WOMBAT”, 3) = “WOM”) (“WOM” = “WOM”) TRUE (10 > X) AND (X > 4) TRUE and TRUE TRUE

  26. 2. Translation Set 3

  27. Translate 7 • Translate Algorithm 3.2 into Visual Basic

  28. Name: SUMN • Givens: N • Change: None • Results: Sum • Intermediates: Value • Definition: • Sum := SUMN (N) • Get N • Let Value = 1 • Let Sum = 0 • Loop When (Value <= N) • Let Sum = Sum + Value • Let Value = Value + 1 • Finish Loop • Give Sum

  29. Name: SUMN • Givens: N • Change: None • Results: Sum • Intermediates: Value • Definition: • Sum := SUMN (N) • Get N • Let Value = 1 • Let Sum = 0 • Loop When (Value <= N) • Let Sum = Sum + Value • Let Value = Value + 1 • Finish Loop • Give Sum Option Explicit 'Written By T. James Sub SUMN() Dim N as Integer Dim Value as Integer Dim Sum as Integer N = InputBox("Give N") Value = 1 Sum = 0 MsgBox("Total is " & Sum) End Sub

  30. Option Explicit 'Written By T. James Sub SUMN() Dim N as Integer Dim Value as Integer Dim Sum as Integer N = InputBox("Give N") Value = 1 Sum = 0 • Name: SUMN • Givens: N • Change: None • Results: Sum • Intermediates: Value • Definition: • Sum := SUMN (N) • Get N • Let Value = 1 • Let Sum = 0 • Loop When (Value <= N) • Let Sum = Sum + Value • Let Value = Value + 1 • Finish Loop • Give Sum Do While (Value <= N) Sum = Sum + Value Value = Value + 1 Loop MsgBox("Total is " & Sum) End Sub

  31. Option Explicit 'Written By T. James Sub SUMN() Dim N as Integer Dim Value as Integer Dim Sum as Integer N = InputBox("Give N") Value = 1 Sum = 0 • Name: SUMN • Givens: N • Change: None • Results: Sum • Intermediates: Value • Definition: • Sum := SUMN (N) • Get N • Let Value = 1 • Let Sum = 0 • Loop When (Value <= N) • Let Sum = Sum + Value • Let Value = Value + 1 • Finish Loop • Give Sum Do While (Value <= N) Sum = Sum + Value Value = Value + 1 Loop MsgBox("Total is " & Sum) End Sub

  32. Option Explicit 'Written By T. James Sub SUMN() Dim N as Integer Dim Value as Integer Dim Sum as Integer N = InputBox("Give N") Sum = 0 • Name: SUMN • Givens: N • Change: None • Results: Sum • Intermediates: Value • Definition: • Sum := SUMN (N) • Get N • Let Value = 1 • Let Sum = 0 • Loop When (Value <= N) • Let Sum = Sum + Value • Let Value = Value + 1 • Finish Loop • Give Sum For Value = 1 to N Sum = Sum + Value Next Value MsgBox("Total is " & Sum) End Sub

  33. Translate 8 • Translate Algorithm 3.5 into Visual Basic

  34. Name: MAX5 • Givens: N • Change: None • Results: Max • Intermediates: Count • LastCount (Constant) • Definition: • Max := Max5(N) Set LastCount = 5 Let Max = -1 Let Count = 1 Loop When (Count <= LastCount) Get N If (N > Max) Let Max = N Let Count = Count + 1 Finish Loop Give Max

  35. Name: MAX5 • Givens: N • Change: None • Results: Max • Intermediates: Count • LastCount(Constant) • Definition: • Max := Max5(N) Option Explicit 'Written By T. James Sub MAX5() Dim N as Integer Dim Max as Integer Dim Count as Integer Const LastCount = 5 Max = -1 Count = 1 Set LastCount = 5 Let Max = -1 Let Count = 1 Loop When (Count <= LastCount) Get N If (N > Max) Let Max = N Let Count = Count + 1 Finish Loop Give Max

  36. Name: MAX5 • Givens: N • Change: None • Results: Max • Intermediates: Count • LastCount (Constant) • Definition: • Max := Max5(N) Option Explicit 'Written By T. James Sub MAX5() Dim N as Integer Dim Max as Integer Dim Count as Integer Const LastCount = 5 Max = -1 Count = 1 Set LastCount = 5 Let Max = -1 Let Count = 1 Loop When (Count <= LastCount) Get N If (N > Max) Let Max = N Let Count = Count + 1 Finish Loop Give Max Do While (Count <= LastCount) N = InputBox("N") If (N > Max) Then Max = N End If Count = Count + 1 Loop MsgBox("Max is " & Max) End Sub

  37. Name: MAX5 • Givens: N • Change: None • Results: Max • Intermediates: Count • LastCount(Constant) • Definition: • Max := Max5(N) Option Explicit 'Written By T. James Sub MAX5() Dim N as Integer Dim Max as Integer Dim Count as Integer Const LastCount = 5 Max = -1 Count = 1 Set LastCount = 5 Let Max = -1 Let Count = 1 Loop When (Count <= LastCount) Get N If (N > Max) Let Max = N Let Count = Count + 1 Finish Loop Give Max Do While (Count <= LastCount) N = InputBox("N") If (N > Max) Then Max = N End If Count = Count + 1 Loop MsgBox("Max is " & Max) End Sub

  38. Name: MAX5 • Givens: N • Change: None • Results: Max • Intermediates: Count • LastCount (Constant) • Definition: • Max := Max5(N) Option Explicit 'Written By T. James Sub MAX5() Dim N as Integer Dim Max as Integer Dim Count as Integer Const LastCount = 5 Max = -1 Set LastCount = 5 Let Max = -1 Let Count = 1 Loop When (Count <= LastCount) Get N If (N > Max) Let Max = N Let Count = Count + 1 Finish Loop Give Max For Count = 1 to LastCount N = InputBox("N") If (N > Max) Then Max = N End If Next Count MsgBox("Max is " & Max) End Sub

  39. 3. Additional Material Visual Basic Material to Remember

  40. Program Layout First statement is Option Explicit Comments follow a single quote ‘ Start with SUB, finish with END SUB Declaration DIM or CONST Type Assignment Statements Variable = Expression Get/Give X = Inputbox() MsgBox Conditional Branch If … End If If … Else … End If If … ElseIf… End If Repetition Do While Loop Do Until Loop For Next VB Material to Remember

  41. Translations

  42. Again??? • We will visit the concept of Again in a future lecture • Again can be accomplished by other methods • Let Again = "Y"; Again = InputBox ("Again Y/N") • Must be "Y" to continue, "Yes", "y" etc will not work • By setting a flag value • SN = InputBox("Enter Student Number or 999 to stop") • Finish Loop When SN = 999

  43. Name: AVERAGE10 Givens: N Change: None Results: AVG Intermediates: Count, Sum, Again Definition: AVG = AVERAGE10(N) Method Let Count = 0 Let Sum = 0 Let Again = True Loop When (Count < 10) AND (Again) Get N Let Sum = Sum + N Let Count = Count + 1 Get Again Finish Loop Let AVG = Sum/Count Give AVG Option Explicit 'Written By T. James Sub Average10() Dim N as Integer Dim Avg as Single Dim Count as Integer Dim Sum as Integer Dim Again as String*1 Count = 0 Sum = 0 Again = "Y" Do While (Count < 10) and _ (Again = "Y") N = InputBox("N") Sum = Sum + N Count = Count + 1 Again = InputBox ("Again Y/N") Loop Avg = Sum/Count MsgBox("Average = " & Avg) End Sub Algorithm 3.3

  44. Name: AVGPOS Givens: N Change: None Results: Avg Intermediates: Again, Sum, Count Definition:Avg := AVGPOS(N) Method Let Sum = 0 Let Count = 0 Loop Get N If (N > 0) Let Sum = Sum + N Let Count = Count + 1 Get Again Finish Loop When Not(Again) Let Avg = Sum/Count Give Avg Option Explicit 'Written By T. James 'Assume range for N is -100 to 100 'Using Flag 999 with N Sub AvgPos() Dim N as Integer Dim Avg as Single Dim Sum as Integer Dim Count as Integer Sum = 0 Count = 0 N = InputBox("Enter First Value") Do If (N > 0) then Sum = Sum + N Count = Count + 1 EndIf N = InputBox("Enter N or 999") Loop Until (N = 999) Avg = Sum/Count MsgBox("Average = "& Avg) End Sub Algorithm 3.4

  45. Homework

  46. In your homework (Lecture 10), you developed the Visual Basic code for: • reversing the digits in a three digit number and adding that number to 500 • determining the eldest of 2 people • calculating a sales commissionFor each of these questions, revise the program to: • Run the reversing multiple times until the user enters the value 999 Using a Do – While – Loop • Run the eldest algorithm until the user indicates “no” to the question “Do you want to continue?” Using a Do – Loop - Until • Run the sales commission algorithm for exactly 10 sales representatives. Using a For – Next

  47. Develop a subroutine to assist a clerk in determining some statistics about students. For each student, she enters the name, gender (M or F), age and marital status (M or S). She wants to determine the number of married men, married women, single women and eligible bachelors (single men over 25). Each time she has completed entry of data for a student, the algorithm should give her a chance to indicate whether she has entered the data for all of the students.

  48. Write a subroutine to determine the closing balance for a teller. The teller enters his opening balance and then a number of transactions. Deposits are entered as positive numbers and withdrawals are entered as negative numbers. He also needs to calculate the number of deposits and the number of withdrawals. The teller indicates that all transactions have been entered by entering a transaction with a value of 0.

  49. Write a subroutine to calculate and display the total gross earnings, tax payable, medical deduction and net earnings for all employees. The user will input the hours worked and the hourly rate of pay for each employee. The gross earnings for each employee can be calculated from this information. The net earnings are calculated by subtracting the tax payable and medical deductions from the gross earnings. Tax payable is 20% of gross pay for the first $300, 30% for the next $200 and 40% thereafter. The medical deduction is calculated as 1% of gross pay. The user will indicate that all employee information has been entered by entering hours worked as 999.

More Related