COMP1730/COMP6730 Programming for Scientists. Control, part 2: Iteration

Similar documents
COMP1730/COMP6730 Programming for Scientists. Functional abstraction, with robots

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

COMP1730/COMP6730 Programming for Scientists. Functions

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

C 101. Davide Vernizzi. April 29, 2011

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Conditionals and Recursion. Python Part 4

DECISION CONTROL AND LOOPING STATEMENTS

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Control Flow: Loop Statements

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

COMP 208 Computers in Engineering

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Text Input and Conditionals

Chapter 4: Making Decisions

Loops / Repetition Statements

Chapter 4: Making Decisions

CSI33 Data Structures

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 2 Writing Simple Programs

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Iteration Part 1. Motivation for iteration. How does a for loop work? Execution model of a for loop. What is Iteration?

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

CS313D: ADVANCED PROGRAMMING LANGUAGE

Problem Solving for Intro to Computer Science

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Loop structures and booleans

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Test #2 October 8, 2015

CS1 Lecture 22 Mar. 6, 2019

CSCI 1101B. Boolean Expressions

Conditional Expressions and Decision Statements

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

CS1 Lecture 5 Jan. 25, 2019

Selection Statement ( if )

CS111: PROGRAMMING LANGUAGE II

YOLOP Language Reference Manual

LECTURE 04 MAKING DECISIONS

COMP1730/COMP6730 Programming for Scientists. Strings

CS1 Lecture 5 Jan. 26, 2018

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

C++ for Python Programmers

the drawcurve() function code comments aimed at helping you in writing the code for the TODOs defined later.

Model Parameter Estimation

DM502 Programming A. Peter Schneider-Kamp.

Control Structures 1 / 17

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Python for Informatics

RCX Tutorial. Commands Sensor Watchers Stack Controllers My Commands

Today's Topics. CISC 458 Winter J.R. Cordy

Introduction to Programming

Computer Simulation And Modeling

204111: Computer and Programming

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

CMSC 201 Spring 2017 Lab 12 Recursion

Chapter 4 C Program Control

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Ambiguity and Errors Syntax-Directed Translation

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Functions and Recursion

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

8. Control statements

CMSC 201 Fall 2018 Lab 04 While Loops

How would you handle the case for when a user selects an option? 1/15/2016 CSE 11 WINTER LEC 6 2

Senthil Kumaran S

Conditionals: Making Choices

FORM 2 (Please put your name and form # on the scantron!!!!)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Introduction to Programming I COS1511 School of Computing Revision Notes

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Conditionals & Loops /

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

Chapter 9: Dealing with Errors

LOOPS. Repetition using the while statement

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Program Testing and Analysis: Manual Testing Prof. Dr. Michael Pradel Software Lab, TU Darmstadt

Accelerating Information Technology Innovation

Local and Global Variables

Cambridge International Examinations Cambridge International Advanced Subsidiary and Advanced Level. Published

Beyond Blocks: Python Session #1

CS21: INTRODUCTION TO COMPUTER SCIENCE. Prof. Mathieson Fall 2017 Swarthmore College

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Chapter 8. Statement-Level Control Structures

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Transcription:

COMP1730/COMP6730 Programming for Scientists Control, part 2: Iteration

Outline * Iteration: The while statement * Simulations. * Common problems with loops.

Iteration

Program control flow Images from Punch & Enbody

Iteration while test: statement statement... statement... UNTIL while test: statement statement... statement... * Iteration repeats a suite of statements. * A test is evaluated before each iteration, and the suite executed (again) if it is true.

Iteration statements in python * The while loop repeats a suite of statements as long as a condition is true. * The for loop iterates through the elements of a collection or sequence (data structure) and executes a suite once for each element. - We ll come back to the for loop later in the course.

The while loop statement while test expression : suite statement(s) 1. Evaluate the test expression (converting the value to type bool if necessary). 2. If the value is True, execute the suite once, then go back to 1. 3. If the value is False, skip the suite and go on to the following statements (if any).

Suites (reminder) * A suite is a (sub-)sequence of statements. * A suite must contain at least one statement! * In python, a suite is delimited by indentation. - All statements in the suite must be preceded by the same number of spaces/tabs (standard is 4 spaces). - The indentation depth of the suite following if / else / while : must be greater than that of the statement. * A suite can include nested suites (if s, etc).

Variable assignment (reminder) * A variable is a name that is associated with a value in the program. * Variable assignment is a statement: var name = expression - Note: Equality is written == (two = s). * A name value association is created by the first assignment to the name; * subsequent assignments to the same name change the associated value.

* For example, an int = 2 + 3 an int = an int * 5 (From pythontutor.com) 1. Evaluate expression 2 + 3 to 5. 2. Set value of an int to 5. 3. Evaluate expression an int * 5 to 25. 4. Set value of an int to 25.

Problem: Counting boxes * How many boxes are in the stack from the box in front of the sensor and up? * While robot.sense color() ==, move the lift up, and count how many times; then move the lift down that many times.

def count boxes(): num boxes = 0 while robot.sense color()!= : num boxes = num boxes + 1 robot.lift up() steps to go = num boxes while steps to go > 0: robot.lift down() steps to go = steps to go - 1 return num boxes

Problem: Solving an equation * Solve f (x) = 0. * The interval-halving algorithm: - if f (m) 0, return m; - if f (m) < 0, set l to m; - if f (m) > 0, set u to m.

return from a loop * A loop (while or for) can appear in a function suite, and a return statement can appear in the suite of the loop. def find box(color): while robot.sense color()!= : if robot.sense color() == color: return True robot.lift up() return False * Executing the return statement ends the function call, and therefore also exits the loop.

Simulation

Problem: How high does the Falcon 9 fly? * Acceleration is thrust (force) divided by mass. * 90% 96% of mass is fuel. * Rocket s engines have about 7.5% more thrust in vacuum than at sea level. Image by SPACEX

Simulation * Approximate the evolution of a complex of coupled processes. * Simulate time by small steps (δt): - At each step, compute the change in each variable over δt using the current values of other variables.

Example: Rocket simulation * Altitude (a): δa = v δt * Velocity (v): δv = acceleration δt * acceleration = (thrust(a)/m) g - assuming thrust(a) grows linearly between sea level pressure and vaccuum (probably wrong). * Mass (m): - at time 0, m = take-off weight. - δm = B δt. - burn rate B = take-off fuel weight / burn time.

Example: The Competitive Lotka-Volterra model of ecology * The change in the population of species i is ( xi + δx i /δt = r i x i (1 )) j i a ijx j K i where - r i is the inherent growth rate of species i; - a ij is the (negative) effect of species j on species i; - K i is the population of species i that the environment can support ( carrying capacity ).

Writing and debugging loops

Repeat while condition is true * A while loop repeats as long as the condition (test expression) evaluates to True. * If the condition is initially False, the loop executes zero times. * If no variable involved in the condition is changed during execution of the suite, the value of the condition will not change, and the loop will continue forever.

Common problems with while loops * Loop never starts: the control variable is not initialised correctly. # find smallest divisor of num: i = 1 while num % i!= 0: i = i + 1 - num % 1 is always 0!

Common problems with while loops * Loop never ends: the control variable is not updated in the loop suite, or not updated in a way that can make the condition false. i = 0 while i!= stop num: i = i + step size - What if stop num < 0? - or step size < 0? - or step size does not divide stop num?