1 / 12

Chapter 6

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

nathan
Download Presentation

Chapter 6

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. Chapter 6 More on looping

  2. We have three types of loops: • While- entrance-controlled • Until- exit controlled • For…Next- counting

  3. 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?

  4. Analyze this... Let x = Val(txtbox.text) Do While x > 5 picbox.Print “hiya” x = x + 1 Loop

  5. 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?

  6. Or this... Do password = InputBox(“Enter password”) Loop Until password = “Melissa”

  7. 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...

  8. 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

  9. 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

  10. 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.

  11. 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

  12. 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.

More Related