Python While Loops.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Copyright © Texas Education Agency, Computer Programming For Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Simple Python Loops Sec 9-7 Web Design.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
GCSE Computing#BristolMet Session Objectives #23 MUST understand what is meant by the programming term iteration SHOULD describe methods of looping used.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
 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.
Learning Javascript From Mr Saem
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
PYTHON IF-STATEMENTS. What you know If something is true, then something happens Example If you heat water to 100 degrees Celsius, then it boils If it.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Adapted from slides by Marty Stepp and Stuart Reges
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 6: Loops.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Think What will be the output?
Topic 5 for Loops -Arthur Schopenhauer
Control Structures
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Loop Control Structure.
Iteration with While You can say that again.
Lecture Notes – Week 3 Lecture-2
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
IST256 : Applications Programming for Information Systems
Module 4 Loops.
Building Java Programs
Looping Topic 4.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Repetition In today’s lesson we will look at:
Lab5 PROGRAMMING 1 Loop chapter4.
Introduction to Repetition Structures
CHAPTER 6: Control Flow Tools (for and while loops)
Building Java Programs
Seating “chart” Front - Screen rows Back DOOR.
Building Java Programs
Chapter 4: Repetition Structures: Looping
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
How to allow the program to know when to stop a loop.
Presentation transcript:

Python While Loops

What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are facing a wall, turn left

Flow of a while-loop while true condition false statement(s) in while body

Python While Loop Template while CONDITION: #code in while block What’s happening above? If the CONDITION is True, then the code in the while block runs At the end of the while block, the computer automatically goes back to the while CONDITION line It checks if the CONDITION is True, then repeats the while block if it is

How do we loop count? How do we run our loop a specific number of times? Loop counters!  It’s just a variable x = 0 Limit the while condition using the loop counter while x < 5: The variable counts the number of times you run x = x + 1

Loop counting example x = 0 while x < 5: print(“hello”) x = x + 1 #shortcut: x += 1 What’s happening above? x is initialized to 0, which is less than 5 Check if x < 5 is True while block runs (notice that x = x + 1 is indented) x increases to 1 (because 0 + 1 = 1 is saved back in x) Go back up to check if the while condition is True

Example x = 0 while x < 10: print(x**2) x += 1 # Execute above code # What is the output? Why?

Another example x = 1 N = 1000 while x < N: print(x) x *= 2 # Execute above code # What is the output? Why?

An infinite loop while True: print(“All work and no play makes Jack a dull boy”) # Execute above code # What is the output? Why?

How do we exit a loop? You can use the keyword break Example: x = 0 while x < 1000000: print(x) if x == 5: break x += 1 What’s happening above? Counter variable “x” increases, but if x == 5, then break exits the loop