More about Iteration Victor Norman CS104. Reading Quiz.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Q and A for Section 5.1 CS 106, Fall While loop syntax Q: The syntax for a while statement is: while _______________ : _____________ A: while :
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer Science 111 Fundamentals of Programming I Iteration with the for Loop.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python Control of Flow.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
More Looping Structures
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Control Structures FOR Statement Looping.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
While Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
For loops in programming Assumes you have seen assignment statements and print statements.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
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.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Chapter 9 Repetition.
Topic: Iterative Statements – Part 1 -> for loop
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Python: Control Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
While Loops in Python.
Python - Loops and Iteration
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Logical Operators and While Loops
Chapter 5 Repetition.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 9 Control Structures.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
IST256 : Applications Programming for Information Systems
More Looping Structures
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Python programming exercise
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Logical Operators and While Loops
Loops and Simple Functions
Another Example Problem
Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations Choose between several.
Python While Loops.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
More Looping Structures
CISC101 Reminders Assignment 3 due today.
While Loops in Python.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Iteration – While Loops
GCSE Computing.
Introduction to Python
Presentation transcript:

More about Iteration Victor Norman CS104

Reading Quiz

For Loop Review Syntax: for in : (i.e., the body) are executed a fixed number of times – the number of items in. “iterates” through -- takes the next value on each time through the loop.

For loop Review continued range() is very useful to create a. range(start, stop, step) When used in place of, let’s you iterate through loop multiple times. E.g., for i in range(20, 31): print(i)

While loop syntax while : “As long as the Boolean expression is True”, do the statements. Why have both for-loop and while-loop in Python? While loop is Indefinite iteration For loop is definite iteration: loop is done a prescribed # of times. No Boolean test is done.

CQ: In a while loop like this: while : Is it a good idea for the statements in the body to change any variables’ values that are used in the Boolean expression?

CQ: What is printed by the following code? (Output is on one line to save space.) x = 6 while x > 4: print(x) x = x - 1

CQ: What is printed by the following code? (Output is on one line to save space.) x = 0 while x < 2: y = 1 while y < 4: print(x + y) y = y + 2 x = x + 1

Exercise: Write code to do this (where user enters any name or q): Enter your name (q to quit): Arthur Hello, Arthur Enter your name (q to quit): Merlin Hello, Merlin Enter your name (q to quit): q Goodbye.

Answer name = input(“Enter your name (q to quit):”) while name != “q”: print(“Hello,” + name) name = input(“Enter your name (q to quit):”) print(“Goodbye.”)

break Statement (not in the book) only can be used inside a loop body instantly stops the loop and jumps to the next statement after the end of the loop. useful for when you have an if in a loop and want to stop e.g., when you are searching and find the item.

continue Statement (not in book) only can be used in body of a loop. instantly jumps back up to the next iteration of the loop. very useful when “filtering” out stuff.

continue Example earthquakes = [ … ] for e in earthquakes: # filter out earthquakes that are too small if magnitude(e) < 5.0: continue # skip this earthquake e # more filters here, perhaps… print(e) # only prints large earthquakes

CQ: What for loop is equivalent to this while loop?: x = 3 while x < 7: print(x) x = x + 2

CQ: What does this code do? inputs = [ … some list of strings … ] for elem in inputs: if elem == ‘q’ or elem == ‘Q’: break if elem[0] == ‘#’: continue doStuffWith(elem)