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.

Slides:



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

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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
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.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
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.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
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.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
Programming: Simple Control Structures Sec 46 Web Design.
GCSE Computing#BristolMet Session Objectives #23 MUST understand what is meant by the programming term iteration SHOULD describe methods of looping used.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
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.
Decision Making: while loops and Boolean Logic. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as.
Sensor Information: while loops and Boolean Logic.
Follow up from lab See Magic8Ball.java Issues that you ran into.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 6: Loops.
Programming: Simple Control Structures
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.
Karel J Robot Chapter 6.
Topic 5 for Loops -Arthur Schopenhauer
Incrementing ITP © Ron Poet Lecture 8.
And now for something completely different . . .
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Looping and Repetition
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?
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
CMPT 102 Introduction to Scientific Computer Programming
Lab5 PROGRAMMING 1 Loop chapter4.
CHAPTER 6: Control Flow Tools (for and while loops)
Chapter 4: Repetition Structures: Looping
Prepared By: Deborah Becker
PROGRAM FLOWCHART Iteration Statements.
Python While Loops.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
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 condition statement(s) in while body false true

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

Example while karel.anyBeepersInBeeperBag(): karel.putBeeper()

Another example while karel.nextToABeeper(): karel.pickBeeper()

An infinite loop while True: karel.turnLeft()

How do we exit a loop? You can use the keyword break Example: while True: if myRobot.facingNorth(): break myRobot.turnLeft() What’s happening above? If the Robot ever faces North, then break exits the loop (skipping the rest of the while block)

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? 1. x is initialized to 0, which is less than 5 2. Check if x < 5 is True 3. while block runs (notice that x = x + 1 is indented) 4. x increases to 1 (because = 1 is saved back in x) 5. Go back up to check if the while condition is True