Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Microsoft® Small Basic
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
1 CSI 101 Elements of Computing Fall 2009 Lecture #4 Using Flowcharts Monday February 2nd, 2009.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
The Wait-Until-Event pattern Has it happened  Yes, it happened!
Nested LOOPS.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
EXERCISE IN CLASS CHAPTER 2. PART 1 SEQUENCE SCENARIO 1 Write an algorithm for a C program, that prompts user to enter total number of umbrellas he/she.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
My Python Programmes NAME:.
CS 100 Introduction to Computing Seminar October 7, 2015.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
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.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Selection Using IF THEN ELSE CASE Introducing Loops.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Quiz # 02 Design a data type Date to hold date
Lesson 4 - Challenges.
Think What will be the output?
Lesson 3 - Repetition.
Starter Write a program that asks the user if it is raining today.
While Loops (Iteration 2)
Repetition Statements
Introduction to pseudocode
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Lesson 09: Lists Class Chat: Attendance: Participation
A LESSON IN LOOPING What is a loop?
Python Basics with Jupyter Notebook
Introduction to Repetition
Introduction to Python
Introduction to Repetition
GCSE Computing:: While Loops
What you need to do… Drag the pieces of code into the correct order like you can see in the EXAMPLE below. print ( “ int input ) = + Hello world chr ord.
Starter Look at the hand-out.
GCSE Computing.
Presentation transcript:

Python Repetition

We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how many times you want to do something, WHILE is used when you don’t.

FOR Loops FOR is used when you know how many times you want to do something. The following program will output the multiplication table for 7 from 1 up to 12 ‘times’. End x < 12 No Yes Start x = 1 x = x + 1 Display x*7

Example Below is a program developed to output the lyrics of the Beatles song ‘I Want to Hold Your Hand’. Write a program to output the lyrics of your favourite song using a For loop for repeated lines.

Loop Counter The variable x in a for loop is used as a loop counter. It contains the number of times the loop has run. See this for yourself by trying out this code:

WHILE Loops FOR is used when you know how many times you want to do something, WHILE is used when you don’t. The following program keeps on asking the user to enter their password until they enter it correctly. End password <> realpass No Yes Start Input Password Display “Correct”

Example The following program keeps on asking the user to enter their password until they enter it correctly. Try it for yourself.