 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

How SAS implements structured programming constructs
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Chapter 2 Writing Simple Programs
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python quick start guide
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
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.
Control Structures FOR Statement Looping.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
CS 100 Introduction to Computing Seminar October 7, 2015.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control flow Ruth Anderson UW CSE 160 Winter
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.
Testing Programs with Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Trace Tables In today’s lesson we will look at:
Control Flow (Python) Dr. José M. Reyes Álamo.
Topic: Iterative Statements – Part 1 -> for loop
Introduction To Repetition The for loop
Lesson 05: Iterations Class Chat: Attendance: Participation
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?
Repetition: the for loop
Topics Introduction to Repetition Structures
Python - Loops and Iteration
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Logical Operators and While Loops
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Michael Ernst UW CSE 140 Winter 2013
Introduction to Programming
IST256 : Applications Programming for Information Systems
Topics Introduction to Repetition Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Control Structures Part 1
Introduction to Repetition Structures
CHAPTER 6: Control Flow Tools (for and while loops)
Logical Operators and While Loops
For loops Taken from notes by Dr. Neil Moore
PROGRAM FLOWCHART Iteration Statements.
Python While Loops.
Introduction to Programming
For Loops Pages
While Loops in Python.
Class code for pythonroom.com cchsp2cs
Starter Look at the hand-out.
Presentation transcript:

 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of code  While loops are controlled by a sentry value

 Make sure the sentry value can evaluate to True, otherwise the while block will never run  Make sure the sentry value can evaluate to False, otherwise the program will never end, resulting in an ‘endless loop’

hungry = True while(hungry == True) : print("Taking a bite") response = input("Still hungry? (y or n)") if (response == "n") : hungry = False print("Well, that was a good hamburger")

secretPassword = “taylorSwift” guessedPassword = “” while (guessedPassword != secretPassword) : guessedPassword = input(“Enter password: ”) if (guessedPassword != secretPassword) : print(“Password incorrect. Try again.”) print(“Password is correct”)

# if statement # if ( ) : # Only executed if condition is true # block A is executed else : # Only executed if condition is false block B is executed # while loop # while ( ) : # Executes over and over as long # as condition evaluates to true block C is executed

# Parent and two-year-old conversation simulator # response = input(“Why won’t you eat your carrots?”) while (response == ‘Because’) : response = input(‘Because why?’) print(‘The real reason: ’, response)

 A ‘for loop’ iterates over a sequence one element at a time  A ‘for loop’ uses uses a variable that gets each successive element of the sequence  Inside the loop body, the loop can then do something with each successive element

 Start with a ‘for’, followed by a variable for each element, followed by ‘in’  Sometimes you need to count. Python provides the range() function for i in range(1, 5) : print(i) Output:

for letter in ‘Python’ : print(letter) Output: P y t h o n

 Write the Python code that uses a while loop to print even numbers between 100 and 200  Now print the even numbers between 100 and 200 using a for loop  Write the Python code that uses a for loop to print each character of “Taylor Swift” on a separate line. Use a for loop