1 / 17

Working with Loops: Goals and Concepts

In this lecture, you will learn about the different types of loops and when to use them, such as pre-test loops, post-test loops, and counter-controlled loops. You will also understand how to use relational operators and trace the execution of loops. Additionally, data validation techniques will be discussed.

jdunbar
Download Presentation

Working with Loops: Goals and Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Loops

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

  3. 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)

  4. Basic Loop Structure RepeatPrompt for and input a number, NumWrite 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. 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. 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.

  7. Post-test Loop Example RepeatPrompt “Who will win this year’s Superbowl?”Input userAnswer Until userAnswer = “Chicago” Write “You gain wisdom.”

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

  9. Pre-test Loop Example Input Password While Password <> “parrothead”Write “What is the password?”Input Password End While Write “Safe unlocked!”

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

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

  12. For Count = 1 Step 1 To 5 Write Count, Count ^ 2 End For For Loop Example 1 CountOutput 1 1 1 2 2 4 3 3 9 4 4 16 5 5 25

  13. For N = 1 Step 2 To 20 Write N End For For Loop Example 2 NOutput 1 1 3 3 5 5 … 17 17 19 19

  14. 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 NumWhile Num <= 0 Write”The number entered must be positive” Write “Enter a positive number ->” Input NumEnd While… the program continues with a valid number in the variable Num

  15. Data Validation Example Write “Enter a positive number ->”Input NumWhile Num <= 0 Write”The number entered must be positive” Write “Enter a positive number ->” Input NumEnd While… the program continues with a valid number in the variable Num

  16. Questions?

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

More Related