SlideServe
Menu
Préférences
© SlideServe · v2026

looping

ave avatar
16 diapositives · 437 Views · Jul 31, 2014

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

looping - slide 1
01 / 16looping
1 / 17

Télécharger la présentation

looping
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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