Loops Upsorn Praphamontripong CS 1110 Introduction to Programming

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Repetition. Examples When is repetition necessary/useful?
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
CS 100 Introduction to Computing Seminar October 7, 2015.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
More about Iteration Victor Norman CS104. Reading Quiz.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
String and Lists Dr. José M. Reyes Álamo.
Topic: Iterative Statements – Part 1 -> for loop
Input and Output Upsorn Praphamontripong CS 1110
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
CS161 Introduction to Computer Science
Python: Control Structures
IF statements.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CMSC201 Computer Science I for Majors Lecture 09 – For Loops
While Loops in Python.
Topics Introduction to Repetition Structures
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
And now for something completely different . . .
PH2150 Scientific Computing Skills
Logical Operators and While Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Decision Structures, String Comparison, Nested Structures
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
SELECTION STATEMENTS (2)
Loops CIS 40 – Introduction to Programming in Python
CS 1111 Introduction to Programming Fall 2018
More Looping Structures
Topics Introduction to Repetition Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Python programming exercise
Let’s all Repeat Together
CS 1111 Introduction to Programming Spring 2019
CHAPTER 6: Control Flow Tools (for and while loops)
Logical Operators and While Loops
CS 1111 Introduction to Programming Spring 2019
Loops and Simple Functions
Simple Branches and Loops
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
More Looping Structures
CSE 231 Lab 2.
REPETITION Why Repetition?
LOOP Basics.
Introduction to Python
Presentation transcript:

Loops Upsorn Praphamontripong CS 1110 Introduction to Programming Spring 2017

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

“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

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

“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

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

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

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

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

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

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

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

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

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

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

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

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

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: 90 80 78 Average: 82.667, B- Input your scores, separated by spaces: 93 87 90 Average: 90.000, A- CS 1110

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: 90 80 78 Average: 82.667, B- Input your scores, separated by spaces: 93 87 90 Average: 90.000, A- CS 1110