We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24
CHAPTER 6: Use DO/Loops and
LISTS AND For/Next statements to
iterate through a loop LOOPS REPETITION STRUCTURES (1 OF 4)
¡ A repetition structure or loop causes one or more
statements to repeat. ¡ The loop contains a condition that controls whether the instructions are repeated. ¡ In VB, the condition can be phrased in one of two ways: ¡ specify the requirement for repeating the instructions - looping condition ¡ specify the requirement for NOT repeating them – loop exit condition ¡ Pretest loop: ¡ the condition is at the top of the loop and is evaluated before the instructions within the loop are processed ¡ the instructions in a pretest loop may never be processed ¡ Posttest loop: ¡ the condition is at the bottom of the loop and is evaluated after the instructions within the loop are processed REPETITION ¡ the instructions in a posttest loop will always be processed at least once STRUCTURES (3 OF 4) REPETITION STRUCTURES (4 OF 4)
¡ VB has three types
of loops: ¡ For … Next loop ¡ Do While loop ¡ Do Until loop FOR … NEXT LOOP (1 OF 6)
¡ You can use a For...Next loop when a section of code
should be executed an exact number of times. FOR … NEXT LOOP (2 OF 6)
Loop Iteration Value of intNumber Process
1 intNumber = 1 Executes the code inside the loop 2 intNumber = 2 Executes the code inside the loop 3 intNumber = 3 Executes the code inside the loop 4 intNumber = 4 Executes the code inside the loop 5 (exists the loop) intNumber = 5 The control variable value exceeds the ending value, so the application exits the For...Next loop. This means the statement(s) following the Next command are executed. FOR … NEXT LOOP (3 OF 6)
• A Step value is the value in a For...Next loop that is added
to or subtracted from the beginning value on each iteration of the loop – Default step value is 1 – Can be positive or negative, contain decimals, or include variables and mathematical expressions FOR … NEXT LOOP (4 OF 6) FOR … NEXT LOOP (5 OF 6)
• You can also assign decimal values to the control variable in a
For...Next loop • The loop ends when the value in the control variable is greater than 4.5 • The Step value is 0.1, which means the value in decNumber increments by 0.1 on each pass through the loop FOR … NEXT LOOP (6 OF 6)
¡ A For...Next loop can also include variables and
mathematical expressions DO … LOOP STATEMENT - PRETEST LOOP (1 OF 4)
¡ In a Do loop, the body of the loop is executed while or
until a condition is true or false. DO … LOOP STATEMENT - PRETEST LOOP (2 OF 4)
Loop Iteration Value of intScore Result of Condition Tested
1 intScore = 0 True 2 intScore = 1 True 3 intScore = 2 True 4 intScore = 3 True 5 intScore = 4 True 6 intScore = 5 False DO … LOOP ¡ You use the While keyword in a looping STATEMENT - condition to specify that the loop body should PRETEST LOOP be processed while (in other words, as long as) (3 OF 4) the condition is true. DO … LOOP ¡ You use the Until keyword in a loop exit STATEMENT - condition to specify that the loop body should PRETEST LOOP be processed until the condition becomes true, (4 OF 4) at which time the loop should stop. DO … LOOP STATEMENT - POSTTEST LOOP (1 OF 3)
¡ The syntax for using the Do…Loop statement to code a
posttest loop DO … LOOP STATEMENT - POSTTEST LOOP (2 OF 3)
Loop Iteration Value of intScore at Value of intScore Result of Condition
¡ Occur when the loop body does not contain an instruction that will make either the looping condition evaluate to False or the loop exit condition evaluate to True. COUNTERS
¡ A counter is a numeric variable used for counting something.
¡ For example, number of employees paid in a week. ¡ The intNum variable in Example 1 is a counter because it keeps track of the number of times the loop instructions are repeated. ¡ An accumulator is a numeric variable used for accumulating (adding together) something. ¡ For example, the total dollar amount of a week’s payroll. ACCUMULATORS ¡ The dblTotal variable in Example 2 is an accumulator because it adds together the scores entered by the user. Operation Example with Example with Result Single Operators Compound Operator
COMPOUND add, subtract, multiply, divide, use modulus OPERATORS or exponents, or concatenate strings, storing the result in the same variable. FOR…NEXT VERSUS DO…LOOP STATEMENTS ¡ When using the Do…Loop statement, you must include statements to declare, initialize, and update the counter variable, and you also must include the appropriate comparison in the Do clause. ¡ In a For…Next statement, the declaration, initialization, comparison, and update tasks are handled by the For clause. NESTED LOOP (1 OF 2) • Any loop can be placed within another loop under the following conditions: – Interior loops must be completely contained inside the outer loop – Must have a different control variable NESTED LOOP (2 OF 2)