Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops Upsorn Praphamontripong CS 1110 Introduction to Programming

Similar presentations


Presentation on theme: "Loops Upsorn Praphamontripong CS 1110 Introduction to Programming"— Presentation transcript:

1 Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Spring 2017

2 Walking and Shaking – if
Given a list of Python experts in this room Upsorn-bot wants to shake hands the Python experts who wear glasses CS 1110

3 “Repeated” Process – if
Walking and shaking Given a list of Python experts in this room Write code that print “shake” if an expert wears glasses and print “no” if an expert does not wear glasses if expert_1 == "wear": print("shake") else: print("no") if expert_2 == "wear": ... if expert_n == "wear": 100 experts = 100 if-else blocks ?? CS 1110

4 Looping (in action) Given a list of Python experts in this room Upsorn-bot wants to shake hands the Python experts who wear glasses 1 2 3 4 CS 1110

5 “Repeated” Process – while loop
Walking and shaking Given a list of Python experts in this room Write code that print “shake” if an expert wears glasses and print “no” if an expert does not wear glasses while # there are more experts # if an expert wears glasses # print "shake" # otherwise # print "no" ctr = 0 while ctr < len(experts): expert = experts[ctr] if expert == "wear": print("shake") else: print("no") ctr += 1 experts = ["wear", "no", "no", "wear", "wear", "no"] CS 1110

6 Common mistake: infinite loop
experts = ["wear", "no", "no", "wear", "wear", "no"] ctr = 0 while ctr < len(experts): if experts[ctr] == "wear": print("shake") else: print("no") # ctr = ctr + 1 shake shake shake shake shake . . . CS 1110

7 Common mistake: index out of range
experts = ["wear", "no", "no", "wear", "wear", "no"] ctr = 0 while ctr >= 0: if experts[ctr] == "wear": print("shake") else: print("no") ctr = ctr + 1 shake no no shake shake no Index out of range CS 1110

8 Looping What is looping? Why do we do looping?
The process of doing something in the same order again and again Why do we do looping? To automated some repeated processes To shorten the code, less time/effort, less chance of errors To make code more readable and maintainable To save memory as few instances are created When should we do looping? How do we write loops? CS 1110

9 Loops in Python while loop – a condition-controlled loop
for loop using the range operator – a count-controlled loop for loop using the in operator with a list Nested loops CS 1110

10 While Loop while condition statements handler to break the condition such that the loop terminates Condition (Boolean expression) index = 1 while index <= 10: print(index) index += 1 index = 1 while True: if index > 10: break print(index) index += 1 Bad condition indented CS 1110

11 While Loop (2) while input("Do you want to continue (Y/N) ? ") == "Y": print("let's continue") done = "" while done != "quit": print("let's continue -- not done yet") done = input("Continue ? (type quit to quit) ") CS 1110

12 While Loop (3) Variable not found !! Infinite loop !!
1 2 3 4 5 6 7 8 cnt = 1 while infinite > -1: print(cnt) cnt += 1 Variable not found !! Infinite loop !! CS 1110

13 For Loop (using in operator)
for iterate_var in list statements animals = ['dog', 'cat', 'fish'] for animal in animals: print('Current animal :', animal) for letter in 'Python': print('Current Letter :', letter) CS 1110

14 Example: for Loop (using in operator)
Walking and shaking Given a list of Python experts in this room Write code that print “shake” if an expert wears glasses and print “no” if an expert does not wear glasses for # expert in list of experts # if an expert wears glasses # print "shake" # otherwise # print "no" for expert in experts: if expert == "wear": print("shake") else: print("no") experts = ["wear", "no", "no", "wear", "wear", "no"] CS 1110

15 For Loop (using range operator)
for iterate_var in range(start, stop, step) statements default = 1 Iterate upto this but not include for num in range(1, 100, 5): print(num) for num in range(1, 100): print(num) CS 1110

16 Example: for Loop (using range operator)
Imagine the user enters a string and a substring. Remove the substring from the string without using any built-in functions/methods besides len() (i.e., you may not use .index()). The substring will be at most one character long string1 = "for loop, using range" string2 = "o" result = "" size1 = len(string1) position = 0 for position in range(0, size1): if string1[position] != string2: result = result + string1[position] position += 1 print("result = " + result) CS 1110

17 While Loop vs. For Loop While loop For loop
Use when you don’t know the number of iterations Need to explicitly handle the termination Use when you know the number of iterations Loop stops as it reaches the end of range, or the end of list i = 5 while i > 0: print(i * i * i) i -= 1 i = 5 for i in range(5, 0, -1): print(i * i * i) CS 1110

18 Exercise (while loop) Write a program using a while loop
Your program will takes the number of courses a user enrolled Your program will take a list of 3 exam grades from a user (for each course), separated by spaces (i.e., each grade is separated by a single space) Calculate the average of the grades for each course, and the letter grade (according to our letter grade system found in the syllabus). The scores are not weighted in any way - just do a straight up average. Print to the screen the average along with the letter grade of each course A run of the program might look like this: Input the number of courses: 2 Input your scores, separated by spaces: Average: , B- Input your scores, separated by spaces: Average: , A- CS 1110

19 Exercise (for loop) Write a program using a for loop
Your program will takes the number of courses a user enrolled Your program will take a list of 3 exam grades from a user (for each course), separated by spaces (i.e., each grade is separated by a single space) Calculate the average of the grades for each course, and the letter grade (according to our letter grade system found in the syllabus). The scores are not weighted in any way - just do a straight up average. Print to the screen the average along with the letter grade of each course A run of the program might look like this: Input the number of courses: 2 Input your scores, separated by spaces: Average: , B- Input your scores, separated by spaces: Average: , A- CS 1110


Download ppt "Loops Upsorn Praphamontripong CS 1110 Introduction to Programming"

Similar presentations


Ads by Google