CS 1111 Introduction to Programming Spring 2019

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

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?
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
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.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
While ( number
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Repetition Statements
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CS1022 Computer Programming & Principles
EKT120 COMPUTER PROGRAMMING
Python Loops and Iteration
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Week of 12/12/16 Test Review.
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Python: Control Structures
ECS10 10/10
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.
Transition to Code Upsorn Praphamontripong CS 1110
Control Structures
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Python - Loops and Iteration
Algorithm and Ambiguity
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
And now for something completely different . . .
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.
Chapter 5 Repetition.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Looping and Repetition
Iteration with While You can say that again.
Decision Structures, String Comparison, Nested Structures
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Nate Brunelle Today: Repetition, A Trip To The Moon
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
3 Control Statements:.
Logical Operations In Matlab.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Repetition In today’s lesson we will look at:
Algorithm and Ambiguity
Fundamentals of visual basic
3.1 Iteration Loops For … To … Next 18/01/2019.
A look at Python Programming Language 2018.
Chapter8: Statement-Level Control Structures April 9, 2019
CHAPTER 6: Control Flow Tools (for and while loops)
CS 1111 Introduction to Programming Spring 2019
Instructor: Craig Duckett
Decisions, decisions, decisions
Repetition Statements (Loops) - 2
Simple Branches and Loops
Python While Loops.
CSE 231 Lab 2.
How to allow the program to know when to stop a loop.
Lecture 8 – Practice Exam
LOOP Basics.
Looping and Repetition
Presentation transcript:

CS 1111 Introduction to Programming Spring 2019 Loops CS 1111 Introduction to Programming Spring 2019 [The Coder’s Apprentice, §7]

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

“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 ??

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?

Loops in Python while loop – a condition-controlled loop Today’s focus while loop – a condition-controlled loop for loop using the in operator with a list for loop using the range operator – a count-controlled loop Nested loops

While Loop while <condition>: <statements> <handler> to break the condition such that the loop terminates index = 1 while index <= 10: print(index) index += 1 Condition (Boolean expression) indented Try: tracing through code with http://www.pythontutor.com/visualize.html#mode=edit

Practice Write a program, using a while loop, that prints the numbers 1, 2, 3, ..., 10 Write a program, using a while loop, that prints the numbers 10, 8, 6, 4, 2 (exactly this order) http://www.cs.virginia.edu/~up3f/cs1111/examples/loop/while-practice-1.txt

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) ")

Practice Imagine you are writing a program to mimic the Simpson kids in the Simpsons’ ride scenario Write a program, using a while loop, that repeatedly asks “Are we there yet?” until the answer is “yes” the program then prints “Yay!!” https://www.youtube.com/watch?v=_73wNsR_Dto

While Loop (3) cnt = 1 while infinite > -1: print(cnt) cnt += 1 Variable not found !!

While Loop (4) 1 2 3 4 5 6 7 8 … 1 2 3 4 5 6 7 8 … cnt = 1 while cnt > -1: print(cnt) cnt += 1 cnt = 1 while True: print(cnt) cnt += 1 Infinite loop !! Infinite loop !!

While Loop (5) list_of_colors = [ “green”, “blue”, “red”, “yellow”] index = 0 while index < len(list_of_colors): print(list_of_colors[index]) index += 1 Write function, using while loop, to sum a list of numbers

Back to Walking and Shaking 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

“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"]

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

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

Practice Write a function, using a while loop, that takes a list of animals and a list of sounds. Use the given animals and sounds and print the “Old MacDonald had a farm” song You may assume that the sizes of both lists are the same (that is, the number of animals and sounds given are the same) https://www.youtube.com/watch?v=LIWbUjHZFTw

For Loop (using in operator) for <iterate_var> in <collection>: <statements> animals = ['dog', 'cat', fish'] for animal in animals: print('Current animal :', animal) for letter in 'Python': print('Current Letter :', letter)

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"]

Practice Write a function, using a for loop, that takes a list of what-to-do. Use the given what-to-do and print the “If you’re happy and you know it” song https://www.youtube.com/watch?v=Im5i7EqZE1A