Conditional Loops Python
-
Upload
primeteacher32 -
Category
Career
-
view
404 -
download
9
Transcript of Conditional Loops Python
• An application sometimes needs to perform repetitive actions like: Summing Numbers(Averages) Entering Multiple Data Entries Prompting the User Till the Correct Value is
Entered A shopping list(scanning through the shopping
list and getting the items 1 by one until the list is down and then you leave)
• When a program repeatedly runs a set of statements it is referred to as a loop, iteration or repetition structure.
• There are several types of looping structures:
While Loops Do-While Loops For Loops
Counter Controlled Loops
Condional Loops
• Loops are similar to conditionals because they run on a true/false value set. The loop continuously runs while the condition is true and terminates when it is false.
• Loops can be run for a desired length or until a flag terminates them
Great technique to reuse code and therefore limit the amount of statements a program needs. Reusing the same conditional arguments for testing instead of posting hundreds.
True False
Question: How many statements are needed for the following?Ask the same question 100 timesStore the answer in a variableCheck whether the answer is one of two
answers
Question: How many statements are needed if we loop the code?
• A While Loop is a loop that executes 0 or more times before terminating.
• Pre-conditional loop
Code: Creating a While Loop:
while (condition statement):statement1
statement2
Debugging Techniques: Setup a counter to keep track of the number of times a
loop runs Set up a counter to count if an event is occurring or the
number of times it occurs Output values each time a loop executes
num = eval(input(“Enter a number less than 10:”))
while (number >= 10):print(“Invalid Entry!”)num = eval(input(“Enter a number less than
10:”))
Counter: a variable that is incremented or decremented each time a loop repeats
Can be used to control execution of the loop (also known as the loop control variable)
Must be initialized before entering loop