Text Copyright (c) 2017 by Dr. E. Horvath

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
JavaScript Loops Please use speaker notes for additional information!
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Review while loops Control variables Example Infinite loop
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
Algorithms and Pseudocode
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Week 1, Day 3 Lazy Coding 29 June 2016.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
UNIT 5 Lesson 15 Looping.
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction to Python
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Introduction To Repetition The for loop
Repetition Structures Chapter 9
The switch Statement, and Introduction to Looping
What to do when a test fails
Copyright (c) 2017 by Dr. E. Horvath
Computer Programming I
Scripts & Functions Scripts and functions are contained in .m-files
Exceptions and files Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Primer 2: Functions and Control Flow
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Exception Handling.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Exceptions and files Taken from notes by Dr. Neil Moore
Repetition Structures
Exceptions CSCE 121 J. Michael Moore
Iteration: Beyond the Basic PERFORM
Module 4 Loops.
Exceptions.
Do … Loop Until (condition is true)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
3.1 Iteration Loops For … To … Next 18/01/2019.
Programming in C# CHAPTER - 7
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Prepared By: Deborah Becker
Loops.
Copyright (c) 2017 by Dr. E. Horvath
Additional control structures
Copyright (c) 2017 by Dr. E. Horvath
Copyright (c) 2017 by Dr. E. Horvath
Python Conditionals: The if statement
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Copyright (c) 2017 by Dr. E. Horvath
CMSC 202 Exceptions 2nd Lecture.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Text Copyright (c) 2017 by Dr. E. Horvath Loops: Infinite Loops In order to execute statements over and over again, use a while loop or a for loop. The simplest kind of loop is the while loop, and an example is as follows while True: print('Welcome to Python with Raspberry PI') This print statement executes until you press ctrl-c and you'll see feedback indicating that there is a KeyboardInterrupt. Although we will not discuss the try-except construct until later in the course, we will introduce it on the following page because it offers a tidy way of exiting from the loop. Text Copyright (c) 2017 by Dr. E. Horvath

Loops: Infinite Loops Escape Handling try: while True: print('Welcome to Python with Raspberry PI') except KeyboardInterrupt: print('Escaping loop') Inside the except block is where you incorporate code to handle a keyboard interrupt caused by pressing ctrl-c. There is nothing to clean up here which is why there is just a print statement. If you want to catch the exception, but you don't want any statement executed, add the pass statement instead. Text Copyright (c) 2017 by Dr. E. Horvath

Loops: Allowing the Program to Sleep If you wish to poll for sensor readings,e.g., temperature, pressure, and humidity, you might use a while loop of the kind shown on the previous two slides. However, continually polling the sensors and processing the data will tax the CPU. A better way is to allow the program to rest using the sleep command. from time import sleep from sense_hat import SenseHat sense = SenseHat() while True: temp = sense.get_temperature() sleep(1) #The argument is in units of seconds Text Copyright (c) 2017 by Dr. E. Horvath

Loops: Sleep and Interrupt Handling A while loop example which incorporates the features discussed on the previous slides is given below. from time import sleep from sense_hat import SenseHat sense = SenseHat() try: while True: temp = sense.get_temperature() sleep(1) except KeyboardInterrupt: print('Escaping loop') Text Copyright (c) 2017 by Dr. E. Horvath

Loops: Finite while loops While loops can also be used to execute for a finite number of times or until some condition no longer exists. Let's consider the first kind. i = 0 while i < 10: print(str(i)) i += 1 If the programmer forgets to increment the dummy index, i, then this will be an infinite loop, unintentionally so. Also, the Python interpreter will throw an error if the programmer neglects to initialize the dummy index. Text Copyright (c) 2017 by Dr. E. Horvath

Loops: Finite while loops Let's suppose the while loop ends only when some condition no longer exists. In the following, the loop executes only as long as the humidity is below 75%. from sense_hat import SenseHat from time import sleep sense = SenseHat() humidity = 0 while humidity < 75: humidity = sense.get_humidity() print('Humidity ',humidity) sleep(2) Text Copyright (c) 2017 by Dr. E. Horvath

Loops: Finite while loops Suppose the humidity never rises above 75% then the program will not escape from the loop until ctrl-c is pressed. The programmer could add an additional condition that will cause the program to exit from the loop after a prescribed number of steps. from sense_hat import SenseHat from time import sleep sense = SenseHat() Humidity = 0 i = 0 while humidity < 75 and i < 1000: humidity = sense.get_humidity() print('Humidity ',humidity) sleep(2) i += 1 Text Copyright (c) 2017 by Dr. E. Horvath

Text Copyright (c) 2017 by Dr. E. Horvath Loops: Breaking out The user is prompted for an elementary particle. If he answers electron, proton, or neutron then the break statement is executed. while True: particle = input('Guess an elementary particle') if particle == 'electron' or particle == 'proton' or \ particle == 'neutron': print('Congratulations! You're right. A(n)', particle, \ 'is an elementary particle') break else: print('Sorry, that's wrong. \ Or maybe that is an elementary particle. Try again.') Text Copyright (c) 2017 by Dr. E. Horvath

Text Copyright (c) 2017 by Dr. E. Horvath Loops: Nested Loops Both dummy indices must be initialized before the while loops are entered. Notice where the indices are initialized and notice where each dummy index is incremented. The dummy index, j, is incremented inside the while j loop and the dummy index, i, is incremented inside the while i loop, but after the while j loop. i = 0 while i < 5: j = 0 while j < 8: print('i= '+ i + ' j= ' + j) j += 2 i += 1 Text Copyright (c) 2017 by Dr. E. Horvath