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 ID: 334332
Download Presentation The PPT/PDF document "CS0004: Introduction to Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CS0004: Introduction to Programming
Repetition – Do Loops Slide2
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
,
NotSlide3
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.Slide4
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
First, it checks if
condition
is true.
If the
condition
is false, then the
statement(s)
are not executed
If the
condition
is true, it executes the
statement(s)
in the loop’s block.
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.Slide5
Pretest Do Loop Flowchart
Is the condition True?
Execute statements within the loop
Execute statements that follow the loop
No
YesSlide6
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 enteredThe 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.Slide7
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?Slide8
Posttest Do Loop
General Form of
Posttest:
Do
statement(s)
Loop While
condition
First, it
executes the
statement(s)
Second, it checks the
condition
If the condition is false, then
the loop endsIf the condition is true, it
goes back to step 1.This is called posttest because it checks the condition at the end.Slide9
Posttest Do Loop Flowchart
Is the condition True?
Execute statements within the loop
Execute statements that follow the loop
No
YesSlide10
Posttest Do Loop Example 1
New Topics:
Posttest
Do
Loop
When to use PosttestSlide11
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.Slide12
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
LoopThis 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.Slide13
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.