Bryan Burlingame 26 September 2018

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Programming with Microsoft Visual Basic th Edition
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
CS0004: Introduction to Programming Repetition – Do Loops.
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
The switch Statement, and Introduction to Looping
Topics Introduction to Repetition Structures
Lesson 1 An Introduction
CS 115 Lecture 8 Structured Programming; for loops
Algorithm and Ambiguity
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Unit# 9: Computer Program Development
Last Class We Covered Data representation Binary numbers ASCII values
Learning to Program in Python
Outline Altering flow of control Boolean expressions
Number and String Operations
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
Lecture 5 Fruitful Functions
Fundamentals of Programming
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Lecture 2 Python Programming & Data Types
Bryan Burlingame 17 October 2018
Introduction to Object-Oriented Programming with Java--Wu
Repetition Structures
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Bryan Burlingame 28 November 2018
Coding Concepts (Basics)
Theory Of Computer Science
CIS 16 Application Development Programming with Visual Basic
The Programming Language L
Lecture 2 Python Programming & Data Types
Introduction to Repetition Structures
CISC101 Reminders All assignments are now posted.
Bryan Burlingame 6 March 2019
For loops Taken from notes by Dr. Neil Moore
Bryan Burlingame 13 March 2019
Loops.
Chapter 4: Repetition Structures: Looping
CISC101 Reminders Quiz 1 marking underway.
Bryan Burlingame Halloween 2018
Bryan Burlingame 17 April 2019
Bryan Burlingame 24 April 2019
The Programming Language L
The structure of programming
Lecture 13 Teamwork Bryan Burlingame 1 May 2019.
Using C++ Arithmetic Operators and Control Structures
Thinking procedurally
Software Development Techniques
Intro to Programming (in JavaScript)
Programming Logic and Design Eighth Edition
Presentation transcript:

Bryan Burlingame 26 September 2018 Lecture 6 Iteration Bryan Burlingame 26 September 2018

Announcements Homework 4 due up front Read Chapter 8 Homework 5 due next week

Learning Objectives Discuss iteration Connect iteration to summation

Review So far, we have discussed Values - numeric and string Operators – a manner to calculate some result from a set of values Variables – the ability to store a value for future use Recall: we can reassign a variable to a different value throughout our program (this will come in useful later, today) Functions – segments of code with defined interfaces we can use to partition the process of programming Recursive functions – functions which call themselves, creating repetition Decision making – choosing which segment of code to run, based on some boundary criterion This defines a “Turing complete language” with these operations, we can direct a computer to do anything a computer can do

Review So far, we have discussed Values - numeric and string Operators – a manner to calculate some result from a set of values Variables – the ability to store a value for future use Recall: we can reassign a variable to a different value throughout our program (this will come in useful later, today) Functions – segments of code with defined interfaces we can use to partition the process of programming Recursive functions – functions which call themselves, creating repetition Decision making – choosing which segment of code to run, based on some boundary criterion This defines a “Turing complete language” with these operations, we can direct a computer to do anything a computer can do

Iteration (Repetition) Iteration: repetition of a computational process We already have recursion, why do we want other ways of repeating code segments? What’s the limitation of recursion?

Iteration (Repetition) Iteration: repetition of a computational process We already have recursion, why do we want other ways of repeating code segments? What’s the limitation of recursion? Recursion is expensive – requires many extra compute cycles and requires the computer to track all the waiting function calls On my install of Jupyter, the stack blows up on the 2,966th recursion attempt

Simple Repetition We can repeat a code segment using a simple repetition recipe based on the for keyword for iterates over the members of a collection of items i.e. a for loop repeats a segment of code one time for each value in a set of values, changing the value of an iterating variable on each loop The variable which changes values is called an iterator We will revisit for many times

range creates a list of integers from 0 to (n-1) Simple Repetition We can repeat a code segment using a simple repetition recipe based on the for keyword for iterates over the members of a collection of items i.e. a for loop repeats a segment of code one time for each value in a set of values, changing the value of an iterating variable on each loop The variable which changes values is called an iterator We will revisit for many times i is the iterator range creates a list of integers from 0 to (n-1)

Iteration for specifically iterates over a collection of items, what if we want to repeat on some other condition? while keyword allows for repetition based on an arbitrary condition Looks like if, but repeats instead of only running once What would happen if the i += 1 line didn’t exist?

Iteration for specifically iterates over a collection of items, what if we want to repeat on some other condition? while keyword allows for repetition based on an arbitrary condition Looks like if, but repeats instead of only running once What would happen if the i += 1 line didn’t exist? The boundary condition is never violated, creating an infinite loop

The * indicates this cell is still running Jupyter Notebook The * indicates this cell is still running

Execution can be terminated with the Stop button Jupyter Notebook Execution can be terminated with the Stop button

Break It is possible to stop a loop partway through, using the break instruction Without the break, this would be an infinite loop

Break It is possible to stop a loop partway through, using the break instruction Without the break, this would be an infinite loop Here, I disagree with the esteemed Mr. Downey Break should be rare, not common We already have ways to invert logic using negation (!, not) By placing the boundary in the middle of the loop, we make debugging an error harder

Series Approximations For many mathematical phenomena, we can approximate the value using a series You may have seen the Taylor series in Calculus I This is one of the fundamental uses of a computer

Series Approximations 𝑒= 𝑘=0 ∞ 1 𝑘! For many mathematical phenomena, we can approximate the value using a series You may have seen the Taylor series in Calculus I This is one of the fundamental uses of a computer Let’s look at Euler’s number Algorithm: e = 0 k = 0 add 1 / k! to e add 1 to k repeat previous two line

Series Approximations 𝑒= 𝑘=0 ∞ 1 𝑘! For many mathematical phenomena, we can approximate the value using a series You may have seen the Taylor series in Calculus I This is one of the fundamental uses of a computer Let’s look at Euler’s number Computers are finite; we must bound this algorithm Once we have the precision we want, stop The change in precision is frequently called epsilon Algorithm: e = 0 k = 0 previous_e = 1 epsilon = 1 while epsilon > 1x10-7 add 1 / k! to e epsilon = e – previous_e previous_e = e add 1 to k

Series Approximations 𝑒= 𝑘=0 ∞ 1 𝑘! For many mathematical phenomena, we can approximate the value using a series You may have seen the Taylor series in Calculus I This is one of the fundamental uses of a computer Let’s look at Euler’s number Computers are finite; we must bound this algorithm Once we have the precision we want, stop The change in precision is frequently called epsilon Algorithm: e = 0 k = 0 previous_e = 1 epsilon = 1 while epsilon > 1x10-7 add 1 / k! to e epsilon = e – previous_e previous_e = e add 1 to k

Stitching it all together 𝑒= 𝑘=0 ∞ 1 𝑘!

Resources Downey, A. (2016) Think Python, Second Edition Sebastopol, CA: O’Reilly Media (n.d.). 3.7.0 Documentation. 6. Expressions — Python 3.7.0 documentation. Retrieved September 11, 2018, from http://docs.python.org/3.7/reference/expressions.html