1 / 16

looping

looping. LOOPING. Used to perform repetitive tasks Tasks use the same set of statements that are executed again and again until a specific condition is met Statements will loop until the condition is false. Do…loop WHILE. A looping structure that evaluates the loop at least one time

ave
Télécharger la présentation

looping

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

  2. LOOPING • Used to perform repetitive tasks • Tasks use the same set of statements that are executed again and again until a specific condition is met • Statements will loop until the condition is false

  3. Do…loop WHILE • A looping structure that evaluates the loop at least one time • A posttest loop – evaluates the condition after the loop has run • Dim intNum as Integer • Do • intNum = intNum + 2 • Loop While intNum < 10

  4. Do while … loop • A looping structure that may or may not evaluate the loop. • A pretest loop - the condition is FIRST, so the loop may never run • Dim intNum as Integer • Do While intNum < 10 • intNum = intNum + 2 • Loop

  5. Infinite loops • Continues forever because the condition is never false • Happens when there is a logic error – will eventually cause a run time error • Dim intNum as Integer = 1 • Do While intNum > 0 • intNum = intNum + 1 • Loop

  6. Input boxes • The Inputbox() function displays an input box with a prompt and a text box • The information entered by the user is the new value for a string variable • Dim strName as String • strName = InputBox(“Enter your name to continue”, “What is your name”) • “Enter your name to continue” is the prompt • “What is your name?” is the title of the InputBox

  7. Accumulator variables • Counters used within loops to update a numeric variable are called accumulator variables • intTotalScore = intTotalScore + intNewScore • Each time this statement is executed, the value of intNewScore is added to the current value of intTotalScore and then this new value is assigned to intTotalScore

  8. flags • A flag, also called a sentinel, is a condition used to signify that a loop should stop executing • It’s an easy way to end a loop

  9. For…next statement • Executes a set of statements a fixed number of times – not Boolean • Forcounter = startToend • statements • Next counter • Counter is initialized to the start number when the loop first executes and is automatically incremented by 1

  10. For…next statement • Dim intCount as Integer • ForintCount = 1 To 10 • Messagebox.Show(intCount) • Next intCount

  11. For…next statement • You create an integer counter to use after the keyword FOR. • The first time the For line executes, your counter is set to whatever is after the “=”. It does NOT reset each time the loop repeats. • Eachtime “Next” is executed, the counter is updated by 1 (default value is 1) • The condition is tested on the FOR line • The condition is still true when counter reaches the value after the “TO”. (Loop still executes) • When the condition is tested false, execution jumps to statement after the NEXT. • Remember, counter is updated to false value and holds that value when you exit the loop.

  12. For…next statement • The loop below executes until intCountis equal to 5, by checking one last time (it is no longer true), jumps to Next and exits the loop. • Dim intCount as Integer • Dim intTotal as Integer • For intCount = 1 To 4 ‘accumulatorintTotal = intTotal + intCountNext intCount • The variable counter “intCount” holds a 5 after the loop ends, but the code inside only executed four times.

  13. For…next statement • You may create the counter variable (intCount) in the For line by using the optionalAs Integer keywords, rather than using a Dim command before the loop. • The lifetime of the variable counter created this way is the lifetime of the loop. (When you exit the loop, the variable counter no longer exists). • For intCount As Integer = 0 To 4intTotal = intTotal + intCountNext intCount • intCountis only in scope in the loop

  14. For…next statement Step Changes the way the counter is incremented. Can be positive or negative Syntax Forcounter = start To end Stepstepnum Statements Nextcounter

  15. For…next statement • The step integer can be a positive number to tell the compiler to increment the counter. • Make sure your start is the low number, the end is the high number. For intCount= 1To 10Step 2 MessageBox.Show("Counting by twos: " & intCount) Next intCount

  16. For…next statement • The step integer can be a negative number to tell the compiler to decrement the counter. • Make sure your start is the high number, the end is the low number. For intCount= 5 To 1 Step -1 MessageBox.Show("Counting Down " & intCount) Next intCount

More Related