Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops.

Similar presentations


Presentation on theme: "CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops."— Presentation transcript:

1 CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops

2 CSCI N201: Programming Concepts Copyright ©2004  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 pre-test loop. When to use a post-test loop.When to use a post-test loop. When to use a counter-controlled (for) loop.When to use a counter-controlled (for) loop. How to use relational operators.How to use relational operators.

3 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.A loop is a programming structure that executes repeatedly until a given condition is met. We categorized loops into two basic groups: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)

4 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.

5 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.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

6 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.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.Programmers use post-test loops when they want the loop body to execute at least once.

7 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.”

8 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.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.Programmers use pre-test loops when they are unsure if the loop might ever need to execute at all.

9 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!”

10 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.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 NumberOutput 3 9 1

11 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.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

12 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 CountOutput 11 1 22 4 33 9 44 16 55 25

13 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science For Loop Example 2 For N = 1 Step 2 To 20 Write N Write N End For NOutput135 …1719

14 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Data Validation Users may enter erroneous data by mistakeUsers 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 ” Input Num End While … the program continues with a valid number in the variable NumPrograms 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 ” Input Num End While … the program continues with a valid number in the variable Num

15 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Data Validation Example Write “Enter a positive number ->” Input Num While Num ” Input Num End While … the program continues with a valid number in the variable Num

16 CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Questions?

17 Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.


Download ppt "CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops."

Similar presentations


Ads by Google