While Loops in Python.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
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.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
While ( number
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
More about Iteration Victor Norman CS104. Reading Quiz.
Lesson #5 Repetition and Loops.
EECS 110: Lec 10: Definite Loops and User Input
Categories of loops definite loop: Executes a known number of times.
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Repetition Structures
REPETITION CONTROL STRUCTURE
Making Choices with if Statements
Lecture 6 Repetition Richard Gesick.
Lesson #5 Repetition and Loops.
Lesson 05: Iterations Class Chat: Attendance: Participation
Repetition-Counter control Loop
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Python - Loops and Iteration
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Logical Operators and While Loops
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.
While Loops.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
Introduction to Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lesson #5 Repetition and Loops.
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Java Programming Loops
Coding Concepts (Basics)
IST256 : Applications Programming for Information Systems
Repetition Structures
Module 4 Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Flowcharts and Pseudo Code
CHAPTER 6: Control Flow Tools (for and while loops)
Logical Operators and While Loops
For loops Taken from notes by Dr. Neil Moore
Lesson #5 Repetition and Loops.
Repetition Statements (Loops) - 2
While Loops in Python.
REPETITION Why Repetition?
Presentation transcript:

While Loops in Python

A bit unfinished business with for loops …

Two kinds of for loops sum = 0 sum = 0 for x in aList: for i in : Element-based Loops Index-based Loops sum = 0 for x in aList: sum += x sum = 0 for i in : sum += ask for two parts of the index-based loop! why is this the egocentric loop? Because we usually use the variable 'i'. Ha ha. Bad humor, indeed! i 1 2 aList = [ 42, -5, 10 ] aList = [ 42, -5, 10 ] x

Two kinds of for loops sum = 0 for x in aList: sum += x sum = 0 Element-based Loops Index-based Loops sum = 0 for x in aList: sum += x sum = 0 for i in range(len(aList)): sum += aList[i] i 1 2 aList = [ 42, -5, 10 ] aList = [ 42, -5, 10 ] x aList[i]

Run loopindex.py

Python ‘for’ loop works well for a collection of known items Python ‘for’ loop works well for a collection of known items. That is, we know the count, or we know the list of items. But what if we don’t know the count, or the list, rather we only know the condition to repeat (or not to repeat) the steps? For example, we want to repeat adding to a sum if the user input is a positive number.

Here is a problem to solve: The user types in a sequence of positive integers. The program is to add up the integers. The user signals the end of the input by entering a non-positive value which should not be part of the sum. sum = 0 v = int( input( ‘Enter a positive integer ( <= 0 to stop) : ‘)) while ( v > 0): sum = sum + v print( ‘Summation is : ‘, sum )

while loops: indefinite, conditional iteration while <condition>: <body> while loops: indefinite, conditional iteration x = 10 if x != 0: print(x) x = x – 1 What are printed on the screen? Python loops: definite (for loops) and indefinite (while loops) While loops have the structure: while <condition>: <body> the <condition> must be a Boolean expression (something that evaluates to true or false) and the <body> can have multiple statements (including if statements, more while loops, etc.) 10 9

while loops: indefinite, conditional iteration while <condition>: <body> x = 10 while x != 0: print(x) x = x – 1 What is the last value printed on the screen? 1 9 10 Python loops: definite (for loops) and indefinite (while loops) While loops have the structure: while <condition>: <body> the <condition> must be a Boolean expression (something that evaluates to true or false) and the <body> can have multiple statements (including if statements, more while loops, etc.)

Patterns of while loop while <condition>: <body> Prime the condition. while <condition>: <body> <update condition> Check the condition. Update the condition. x = 10 while x != 0: print(x) x = x – 1 From logical point of view, while loop has three components. Prime the condition; [before] Check the condition; [in] Update the condition. [in]

Extreme Looping What does this code do? print('It keeps on') while True: print('going and') print('Phew! I\'m done!')

Will the same thing appear every time this is run? Making our escape! Will the same thing appear every time this is run? import random escape = 0 while escape != 42: print('Help! Let me out!') escape = random.choice([41,42,43]) print('At last!')

Count the iterations… import random escape = 0 while escape != 42: What modifications do we need to make to count the number of iterations and report it? import random escape = 0 while escape != 42: print('Help! Let me out!') escape = random.choice([41,42,43]) print('At last!') count = 0 before the loop count += 1 inside the loop print out the count after the loop Make it harder by creating more choices? Allow an escape every nth iteration? Etc. how could we make it easier/harder to escape?

Count the iterations… import random escape = 0 count = 0 What modifications do we need to make to count the number of iterations and report it? import random escape = 0 count = 0 while escape != 42: count += 1 print('Help! Let me out!') escape = random.choice([41,42,43]) print('At last!') count = 0 before the loop count += 1 inside the loop print out the count after the loop Make it harder by creating more choices? Allow an escape every nth iteration? Etc. how could we make it easier/harder to escape? Try out escape.py

Exercises Write a for loop to print the first n values of multiple of 3 Write a while loop that takes a collection of positive integers and computes the sum. The loop stops when a zero or negative number is entered. Compute and print the sum and average of this collection of numbers.