120 likes | 196 Views
Chapter 6. More on looping. We have three types of loops:. While - entrance-controlled Until - exit controlled For…Next - counting. Questions to ask about loops. Will they terminate? Initialization of variables involved in condition
E N D
Chapter 6 More on looping
We have three types of loops: • While- entrance-controlled • Until- exit controlled • For…Next- counting
Questions to ask about loops • Will they terminate? • Initialization of variables involved in condition • Updating of these variables inside the loop body (while & until) • If we have a choice of 2 different loops, are they equivalent?
Analyze this... Let x = Val(txtbox.text) Do While x > 5 picbox.Print “hiya” x = x + 1 Loop
Let x = Val(txtbox.text) Do While x < 5 picbox.Print “hiya” x = x + 1 Loop Let x = Val(txtbox.text) Do picbox.Print “hiya” x = x + 1 Loop Until x >= 5 What about these 2 loops?
Or this... Do password = InputBox(“Enter password”) Loop Until password = “Melissa”
Let sum = 0 For i = 1 to 10 sum = sum + i Next i picbox.Print i, sum Let sum = 0 For i = 1 to 10 sum = sum + i picbox. Print i, sum Next i Compare...
Calculate a running sum from the user (Version 1) Let sum = 0 For t = 1 to 5 w = Val(InputBox(“Enter a number.”)) sum = sum + w Next t
Calculate a running sum from the user (Version 2) sum = 0 w = InputBox(“Enter a number. Hit ENTER to stop”) Do While w < > “” wval = Val(w) sum = sum + wval w = InputBox(“Enter a number. Hit ENTER to stop.”) Loop picbox.Print sum
When do we need loops?(…and what kind do we need?) • Find the position of the first occurrence of a blank in a string • Find all positions where blanks occur in a string.
Nested loops For j = 1 to 3 picbox.Print “New line: “ For k = 1 to 2 picbox.Print “j = “; j; “k = “; k Next k Next j
Homework Due Tuesday. pg 287 # 35, 39. For #35 fill the rectangle (do not make it hollow). So if the number is 4, your output should look like this: **** **** **** **** Also, ask for a number between 3 and 15(inclusive) and test that it is within this range and that it is an integer.