110 likes | 325 Views
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. A looping structure that evaluates the loop at least one time
E N D
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 • A looping structure that evaluates the loop at least one time • Dim intNum as Integer • Do • intNum = intNum + 2 • Loop While intNum < 10
Do while … loop • A looping structure that may or may not evaluate the loop. • The condition is FIRST • Dim intNum as Integer • Do While intNum < 10 • intNum = intNum + 2 • Loop
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 • 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”, “Name”) • “Enter your name” is the prompt • “Name” is the title of the InputBox
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 • 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 • 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 statement • Dim intNumber as Integer • ForintNumber = 1 To 10 • Messagebox.Show(intNumber) • Next intNumber