Working with Loops - Department of Computer and Information

advertisement
Working with Loops
CSCI N201: Programming Concepts
Copyright ©2005  Department of Computer & Information Science
Goals
By the end of this lecture, you should
understand ...
• When to use a pre-test loop.
• When to use a post-test loop.
• When to use a counter-controlled (for)
loop.
• How to use relational operators.
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Introducing Loops
• A loop is a programming structure that
executes repeatedly until a given condition
is met.
• We categorized loops into two basic
groups:
– Loops that depend on some TRUE/FALSE condition
(pre-test & post-test loops)
– Loops that depend on reaching a maximum number
of iterations or “counts” (counter-controlled loops)
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Basic Loop Structure
Repeat
Prompt for and input a number, Num
Write num
Until Num = 0
Write “Done”
– The body of the loop executes repeatedly until the
user enters a 0. At that point the loop exits; the
statement that follows the loop then executes.
– Note the indentation of code, which makes it easier to
read.
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Relational Operators
• Conditions that determine whether a loop
is reentered or exited are usually
constructed with relational operators.
=
<>
<
>
<=
>=
equal to
not equal to
less than
greater than
less than or equal to
greater than or equal to
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Post-test Loops
• When encountering a post-test loop, a
computer tests the condition after the loop
body executes.
• Programmers use post-test loops when
they want the loop body to execute at
least once.
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Post-test Loop Example
Repeat
Prompt “Who will win this
year’s Superbowl?”
Input userAnswer
Until userAnswer = “Chicago”
Write “You gain wisdom.”
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Pre-Test Loops
• When encountering a pre-test loop, a
computer tests the condition before the
loop body executes.
• Programmers use pre-test loops when
they are unsure if the loop might ever
need to execute at all.
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Pre-test Loop Example
Input Password
While Password <> “parrothead”
Write “What is the password?”
Input Password
End While
Write “Safe unlocked!”
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Trace (Walk through) a Loop
• It is impossible to see what a loop is doing
without tracing it to see how it works. Suppose
the user enters 3, 1, -1.
Input Number
While Number > 0
Write Number ^ 2
Input Number
End While
Number
Output
3
9
1
1
-1
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
For Loops
• Because they are so common, most
programming languages include a distinct
structure for easily building them, called a
for loop.
For Counter = InitialValue Step Increment To LimitValue
Body of loop
End For
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
For Loop Example 1
For Count = 1 Step 1 To 5
Write Count, Count ^ 2
End For
Count
Output
1
1
1
2
2
4
3
3
9
4
4
16
5
5
25
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
For Loop Example 2
For N = 1 Step 2 To 20
Write N
End For
N
Output
1
1
3
3
5
5
…
17
17
19
19
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Data Validation
• Users may enter erroneous data by mistake
• Programs should include statements that check,
or validate that the value is in a proper range,
and request the user to re-enter invalid data.
Write “Enter a positive number ->”
Input Num
While Num <= 0
Write”The number entered must be positive”
Write “Enter a positive number ->”
Input Num
End While
… the program continues with a valid number in the variable Num
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Data Validation Example
Write “Enter a positive number ->”
Input Num
While Num <= 0
Write”The number entered must be positive”
Write “Enter a positive number ->”
Input Num
End While
… the program continues with a valid number in the variable Num
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Questions?
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Resources
• Venit, Stewart. Extended Prelude to
Programming: Concepts and Design.
Scott/Jones, Inc., 2002.
CSCI N201: Programming Concepts
Copyright ©2004  Department of Computer & Information Science
Download