CS 106, Winter 2009 Class 14, Section 4

21
1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]

description

CS 106, Winter 2009 Class 14, Section 4. Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]. 1. Assignment 5. In Assignment 5 you will practice with procedures and functions - PowerPoint PPT Presentation

Transcript of CS 106, Winter 2009 Class 14, Section 4

Page 1: CS 106, Winter 2009 Class 14, Section 4

1

CS 106, Winter 2009Class 14, Section 4

Slides by: Dr. Cynthia A. Brown, [email protected] section 4: Dr. Herbert G. Mayer, [email protected]

Page 2: CS 106, Winter 2009 Class 14, Section 4

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.

Page 3: CS 106, Winter 2009 Class 14, Section 4

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

Page 4: CS 106, Winter 2009 Class 14, Section 4

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

Page 5: CS 106, Winter 2009 Class 14, Section 4

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

Page 6: CS 106, Winter 2009 Class 14, Section 4

6

Possible code…

num = Cint(txtInput.Text)strNum = txtInput.TextlstMult.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) )

Page 7: CS 106, Winter 2009 Class 14, Section 4

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

Page 8: CS 106, Winter 2009 Class 14, Section 4

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 loopnum = Cint(txtInput.Text)strNum = txtInput.TextlstMult.Items.Clear()Forj = I To 12lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _CStr(num * j))Next

Page 9: CS 106, Winter 2009 Class 14, Section 4

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.TextlstMult.Items.Clear()Forj = I To num ‘the limits can be variables or expressionslstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _CStr(num * j))Next

Page 10: CS 106, Winter 2009 Class 14, Section 4

10

For Loop Syntax

ForcVar = sValToeValstatementsNext

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

Page 11: CS 106, Winter 2009 Class 14, Section 4

11

Let’s step through this with an example…

Page 12: CS 106, Winter 2009 Class 14, Section 4

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

Page 13: CS 106, Winter 2009 Class 14, Section 4

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.TextlstMult.Items.Clear()j = 1Do While j<= 12lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _CStr(num * j))j = j + 1 ‘what would happen if I forgot this line?Loop

Page 14: CS 106, Winter 2009 Class 14, Section 4

14

Do-While Example

• Here’s the n by n version

num = Cint(txtInput.Text)strNum = txtInput.TextlstMult.Items.Clear()j = 1Do While j<= num ‘note the upper bound is now a variablelstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _CStr(num * j))j = j + 1 Loop

• What happens if num = 0?

Page 15: CS 106, Winter 2009 Class 14, Section 4

15

Do Loop Syntax (While)

Do While conditionstatement(s)Loop

(Loop statements may never be done.)

Condition true?

Execute statements within the loop.

Execute statementsthat follow the loop.

No

Yes

Page 16: CS 106, Winter 2009 Class 14, Section 4

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.

Page 17: CS 106, Winter 2009 Class 14, Section 4

17

Do-Until Example

• Here’s the multiplication example using a Do-until loop

num = Cint(txtInput.Text)strNum = txtInput.TextlstMult.Items.Clear()j = 1Do lstMult.Items.Add(strNum&“ x “ &CStr(j) &“ = “ & _CStr(num * j))j = j + 1 Loop Untilj> 12

Page 18: CS 106, Winter 2009 Class 14, Section 4

18

Do Loop Syntax (Until)

Dostatement(s)Loop Until condition

(Loop statements are always done at least once.)

Condition true?

Execute statements within the loop.

Execute statementsthat follow the loop.

No

Yes

Page 19: CS 106, Winter 2009 Class 14, Section 4

19

BREAK

10 minutes

Page 20: CS 106, Winter 2009 Class 14, Section 4

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.

Page 21: CS 106, Winter 2009 Class 14, Section 4

21

Example: Complete Multiplication Table

Dimj,kAs IntegerDimtableLineAs StringForj= 1 To 12tableLine = “”

Fork= 1 To 12tableLine = tableLine&CStr(j) &“ x “ _&CStr(k) &“ = “ &CStr(j*k) &“ “Next‘klstMult.Items.Add(tableLine)Next‘j