170 likes | 178 Views
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.
E N D
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.
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)
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.
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
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.
Post-test Loop Example RepeatPrompt “Who will win this year’s Superbowl?”Input userAnswer Until userAnswer = “Chicago” Write “You gain wisdom.”
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.
Pre-test Loop Example Input Password While Password <> “parrothead”Write “What is the password?”Input Password End While Write “Safe unlocked!”
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
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
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
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
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
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
Resources • Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.