looping
Download
Skip this Video
Download Presentation
looping

Loading in 2 Seconds...

play fullscreen
1 / 16

looping - PowerPoint PPT Presentation


  • 164 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' looping' - ave


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
looping1
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
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
do while loop
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
infinite loops
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
input boxes
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
accumulator variables
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
flags
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
for next statement
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
for next statement1
For…next statement
  • Dim intCount as Integer
  • ForintCount = 1 To 10
  • Messagebox.Show(intCount)
  • Next intCount
for next statement2
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.
for next statement3
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.
for next statement4
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
for next statement5
For…next statement

Step

Changes the way the counter is incremented.

Can be positive or negative

Syntax

Forcounter = start To end Stepstepnum

Statements

Nextcounter

for next statement6
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

for next statement7
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

ad