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.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Python Control Flow statements There are three control flow statements in Python - if, for and while.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Mastery Objective: Students will understand how to use while loops in computer programming.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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,
For loops in programming Assumes you have seen assignment statements and print statements.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Course A201: Introduction to Programming 09/16/2010.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
 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.
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.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
FOP: While Loops.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Repetition Structures Chapter 9
Guide to Programming with Python
Topics Introduction to Repetition Structures
Python: Control Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
Chapter 5: Repetition Structures
Topics Introduction to Repetition Structures
ITM 352 Flow-Control: Loops
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structures - Repetition
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.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Arrays, For loop While loop Do while loop
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
3 Control Statements:.
IST256 : Applications Programming for Information Systems
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Control Structures Part 1
CHAPTER 6: Control Flow Tools (for and while loops)
CHAPTER 21 LOOPS 1.
More Loops Topics Counter-Controlled (Definite) Repetition
Topics Introduction to Repetition Structures
More Loops Topics Counter-Controlled (Definite) Repetition
Python While Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
LOOP Basics.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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 return the string in a different case each time (uppercase, lowercase, swapcase, titlecase, etc.)

While loops and for loops Loop there it is! While loops and for loops

While loops While some condition is true, repeat something

What’s a while loop Runs while a certain condition is true Examples: number = int(input("enter a number")) while number !=5: number = int(input(“Guess another number”))

Three-Year-Old Simulator Open the code of the Three-Year Old Simulator in the Chapter 3 Folder in IDLE What is happening?

What’s happening?

Examining the while Loop Syntax very similar to if statement : Indent If condition is true, the block is executed Executes block over and over and over until the condition is false

Initializing the Sentry Variable Often, while loops are controlled by a sentry variable Variable used in the condition and compared to some other value or values. What is the sentry variable in the Three-Year- Old Simulator? What is it compared to?

Checking the Sentry Variable Make sure that its possible for the while condition to evaluate to true at some point Otherwise the block will never run Example: response = “Because." while response != "Because.": response = input("Why?\n")

Practice What do you think is happening in the following code? Run the code to determine if you were correct! count = 0 while (count < 9): print ('The count is:', count) count = count + 1 print ("Good bye!“)

Using else Statement with Loops Python allows you to have an if and an else statement associated with a loop statement. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

You try it: count = 0 while count < 5: print (count, " is less than 5“) count = count + 1 else: print (count, " is not less than 5“)

Avoiding Infinite Loops Make sure your loop will end at some point Infinite loop – a loop that never stops Example: counter = 0 while counter <= 10: print (counter) What will happen?

Creating Intentional Infinite Loops Are infinite loops always a mistake? Intentional infinite loops – infinite loops with an exit condition built in to the loop body

Exiting your loop You might face a situation in which you need to exit a loop completely when an external condition is triggered or there may also be a situation when you want to skip a part of the loop and start next execution. Python provides break and continue statements to handle such situations and to have good control on your loop.

The break Statement terminates the current loop resumes execution at the next statement

The continue statement “jump back to the top of the loop” At the top, while condition is tested Loop entered again if it evaluates to true

Open the Finicky Counter Program in the Chapter 3 Folder What’s happening in the following code?

What’s happening?

Finicky Counter Program Counts from 1 to 10 using an intentional infinite loop. Its finicky because it doesn’t like the number 5 and skips it. Technically the loop goes on forever, unless there is an exit condition from that loop in the loop body. When the loop hits 11, it “breaks out of the loop” and the loop ends. When count = 5, the program does not get to the print(count) statement. Instead it goes right back to the stop of the loop so that 5 is never printed.

Hour of Code Review! Log on to https://hourofcode.com/grokeliza Build a friendly chatbot called "Eliza". Can she fool your friends into thinking she's a human?

Independent work Open the Guess My Number file from the chapter 3 folder Read about the guess my number game on pages 81-84 Complete Challenges # 2 and 3 on page 85

Python Loops

Other loops Python programming language provides following types of loops to handle looping requirements. Loop type Description While loop Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. For loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Nested loop You can use one or more loop inside any another while, for or do..while loop.

The for loop Repeats code, but not based on a condition Repeats part of a program based on a sequence (ordered list of things) Repeats its loop body for each element of the sequence, in order. When it reaches the end of the sequence, it loops again

Loopy String Program

All sequences are made up of elements A string is a sequence in which each element is one character

If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.