Presentation on theme: "CS0004: Introduction to Programming Repetition – Do Loops."— Presentation transcript:
CS0004: Introduction to Programming Repetition – Do Loops
Review A condition is… a statement that can (but does not have to) include relational or logical operators that results to either True or False Relational Operators… compare two entities. Returns either True or False. =, <>,, = Logical Operators… combine two or more conditions. Return either True or False. And, Or, Not
Loops So far we have used conditions in making decisions that execute a sequence of statements once. If condition Then statement(s) End If However, we can use conditions to execute a sequence of statements multiple times. A loop is used to repeatedly execute a sequence of statements a number of times. Each repetition of the loop is called a pass, or iteration. Loops can be used for validation, computing naturally repetitive calculations, take in a variable amount of data from the user, or many other tasks.
Do Loops Do Loops execute the code they contain until a condition is false. Do Loops come in two forms: Pretest and Posttest. General Form of Pretest: Do While condition statement(s) Loop 1. First, it checks if condition is true. A. If the condition is false, then the statement(s) are not executed B. If the condition is true, it executes the statement(s) in the loop’s block. a. Then, it goes back to step 1. Said another way, the statement(s) are executed until condition is false. This is called Pretest because the condition comes first.
Pretest Do Loop Flowchart Is the condition True? Execute statements within the loop Execute statements that follow the loop No Yes
Pretest Do Loop Examples 1, 2 and 3 New Topics: Pretest Do Loop Looping used for validation Looping used for variable length user input Notes: In Example 3: count is called a counter variable. Counter Variables count how many times a loop repeats. sum is called an accumulator variable. Accumulator Variables reflect the total (accumulation of) work done by all the repetitions done in the loop. In this case it was the sum of all numbered entered The loop in this case is called a sentinel-controlled loop. A Sentinel-Controlled Loop is broken out of when a variable in its condition gets a certain value. When -1 is entered for num, -1 is called an sentinel value. Sentinel Values create conditions where a sentinel-controlled loop stops repeating.
Pretest Do Loop Example 4 New Topic: Looping for calculations Note: Only use looping for calculations when you cannot easily do the calculations without looping. How would we do Example 4 without looping?
Posttest Do Loop General Form of Posttest: Do statement(s) Loop While condition 1. First, it executes the statement(s) 2. Second, it checks the condition A. If the condition is false, then the loop ends B. If the condition is true, it goes back to step 1. This is called posttest because it checks the condition at the end.
Posttest Do Loop Flowchart Is the condition True? Execute statements within the loop Execute statements that follow the loop No Yes
Posttest Do Loop Example 1 New Topics: Posttest Do Loop When to use Posttest
Until Keyword In both forms of the Do Loop, you can replace While with Until While loops while the condition is True Until loops until the condition is True Do While num <> -1 statement(s) Loop Is the same as… Do Until num = -1 statement(s) Loop Until may make more sense in some sentinel-controlled loops.
Infinite Loops An infinite loop is a logical error where a loop has no way of reaching the condition where the loop stops executing, and therefore repeats indefinitely. For example: Dim num As Integer = 1 Do While num < 1 num += 1 Loop This loop will repeat forever because num will never be less than 1. This may be a simple example, but they can be more complex and harder to spot. If your program seems to stall at about the time a loop is executing, look to see if you have created an infinite loop. To stop a program from running while it is in an infinite loop, hit the “Stop Debugging” button.
Closing Notes: You can end a loop prematurely by using the Exit Do statement in the body of a loop. As soon as Exit Do is executed, execution jumps immediately to the statement following the Loop statement. Just like with an if statement, variables declared inside of a loop have block-level scope, meaning they cannot be used outside of the loop. Be careful though, these variables are then going to be declared EVERY iteration of the loop.