1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
2 Assignment 5 In Assignment 5 you will practice with procedures and functions The assignment statement and examples are on the website. If you compare the code for Ice Cream Store version 2 and 3, you will be changing the Plane Ticket application in the same way, so that the end result does the same thing as the original but uses general procedures and functions.
3 Chapter 6: Repetition Repetition of the same or similar actions is often needed in process design Three styles of repetition: – Repeat for a fixed number of times – Repeat as long as (while) a certain condition is true – Repeat until a certain condition is true
4 Examples Keep ringing up items as long as (while) the customer has more Keep adding numbers until you get to the end of the list Cut a paycheck for each employee in the roster
5 Printing a Multiplication Table Our job: input a number between 1 and 12 in a text box Print a multiplication table for this number in a list box This is possible but painful with our current set of tools
6 Possible code… num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() lstMult.Items.Add(strNum&“ x 1 = “ &CStr(num * 1) ) lstMult.Items.Add(strNum&“ x 2 = “ &CStr(num * 2) ) lstMult.Items.Add(strNum&“ x 3 = “ &CStr(num * 3) ) … lstMult.Items.Add(strNum&“ x 12 = “ &CStr(num * 12) )
7 Ugh! This is clumsy and unbearably repetitive If we wanted to change the upper limit in some way (say do up to 8 *8, or 10*10, instead of going to 12 each time), we would need even uglier code We couldn’t even do some very natural tasks Luckily, VB has some nice repetition constructs
8 For loop Repetitions are called loops in VB A For loop is used when we can determine the number of repetitions before starting the loop num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() Forj = I To 12 lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) Next
9 For loop version 2 Let’s modify the previous example so it prints a multiplication table up to N * N, instead of going to 12. num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() Forj = I To num ‘the limits can be variables or expressions lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) Next
10 For Loop Syntax ForcVar = sValToeVal statements Next Here cVar is the control variable, sVal is the start value for cVar, and eVal is the end value for cVar cVar>eVal? Execute loop statements Increment cVar yes no cVar = sVal
11 Let’s step through this with an example…
12 The Do-While Concept Sometimes we can’t tell in advance how many times a loop will need to execute Or, it might just be clearer to use a logic that checks as we go along In that case we can use a Do loop instead of a For loop
13 Do-While Example Let’s try the same program we did with the For loop, but this time with a Do-While loop num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() j = 1 Do While j<= 12 lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) j = j + 1 ‘what would happen if I forgot this line? Loop
14 Do-While Example Here’s the n by n version num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() j = 1 Do While j<= num ‘note the upper bound is now a variable lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) j = j + 1 Loop What happens if num = 0?
15 Do Loop Syntax (While) Do While condition statement(s) Loop (Loop statements may never be done.) Condition true? Execute statements within the loop. Execute statements that follow the loop. No Yes
16 The Do-Until Variation Instead of testing at the beginning of the loop, we can test at the end. This is useful because, many times, we want to do the loop code at least once, no matter what. We’ll mostly focus on the Do-While and For loops.
17 Do-Until Example Here’s the multiplication example using a Do-until loop num = Cint(txtInput.Text) strNum = txtInput.Text lstMult.Items.Clear() j = 1 Do lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _ CStr(num * j)) j = j + 1 Loop Untilj> 12
18 Do Loop Syntax (Until) Do statement(s) Loop Until condition (Loop statements are always done at least once.) Condition true? Execute statements within the loop. Execute statements that follow the loop. No Yes
19 BREAK 10 minutes
20 Nested Loops Remember how if statements can be “nested”: you can have an if inside another if We can also put whatever we want inside a loop, including another loop If we do this, the inner loop is executed completely, for all of its repetitions, each time the outer loop is executed once.
21 Example: Complete Multiplication Table Dimj,kAs Integer DimtableLineAs String Forj= 1 To 12 tableLine = “” Fork= 1 To 12 tableLine = tableLine&CStr(j) &“ x “ _ &CStr(k) &“ = “ &CStr(j*k) &“ “ Next‘k lstMult.Items.Add(tableLine) Next‘j