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.

Slides:



Advertisements
Similar presentations
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Advertisements

 Control structures  Algorithm & flowchart  If statements  While statements.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Computer Science 1620 Loops.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
PROGRAMMING In Lesson 5. RECAP  Complete the starter activity.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
An Introduction to Textual Programming
Control Structures FOR Statement Looping.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Chapter 5 Repetition or loop structure. What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces)
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.
Chapter 4 Selection Structures: Making Decisions.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
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.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Course A201: Introduction to Programming 09/16/2010.
22/11/ Selection If selection construct.
Design of Bio-Medical Virtual Instrumentation Tutorial 2.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
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.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Decision Structures, String Comparison, Nested Structures
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 7 Problem Solving with Loops
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
31/01/ Selection If selection construct.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Conditionals.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lesson 05: Iterations Class Chat: Attendance: Participation
Introduction to Programming
Think What will be the output?
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Repetition-Sentinel,Flag Loop/Do_While
While Loops in Python.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Print slides for students reference
Writing Functions( ) (Part 5)
Repetition Statements
More Loops.
An Introduction to VEX IQ Programming with Modkit
Java Programming Loops
IST256 : Applications Programming for Information Systems
3.1 Iteration Loops For … To … Next 18/01/2019.
Loop Strategies Repetition Playbook.
Python programming exercise
CISC101 Reminders All assignments are now posted.
Flowcharts and Pseudo Code
Java Programming 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.
While Loops in Python.
Iteration – While Loops
Presentation transcript:

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 2 etc. Stop  How can we do this? number = 1 while the number <= 1000 display the number increase the number 2 of 16

Review: Display Numbers up to 1000  So here is some pseudo code to display all the numbers up to 1000: Start number = 1 while number <= 1000 display number number = number + 1 Stop Why do we need to do this? 3 of 21 3 of 16

Review: Display Numbers up to 1000 What is happening here? 4 of 21 4 of 16

Python: The while loop  In Python we use the while loop for iteration We start by declaring and initializing a number variable This tells Python to execute the following code block while the condition is true This colon is essential, it indicates the start of a block of code. These tabs indicate the while loop code block – just like with conditions. Try this This line is essential, why? 5 of 16

Exercise Now change your code so that it 1. Displays numbers from 1 to Displays numbers from 0 to 40, counting in two’s 3. Displays numbers from 10 down to 0 4. Displays numbers from 20 down to 0 in two’s Save all these as separate programs so that you have them for later reference 6 of 16

Review: Get Five Numbers  Problem: Allow the user to enter 5 numbers Start count = 0 while count < 5 get number count = count + 1 Stop Why < not <= ? 7 of 16

Review: Get Five Numbers 8 of 21 8 of 16

Review: Total Five Numbers  Now let’s also display the total of those numbers Start count = 0 total = 0 while count < 5 get number count = count + 1 total = total + number display total Stop Here is the pseudo code to ask the user for five numbers We need a total, initially set to zero We need to add the number to that total At the end, we need to display the final total 9 of 16

Total Five Numbers Review: Total Five Numbers 10 of 16

Python: Total Five Numbers Start count = 0 total = 0 while count < 5 get number count = count + 1 total = total + number display total Stop Pseudo Code Try this 11 of 16

Your Turn  Write a program to: Ask the user for ten numbers Display the total and average of these numbers 12 of of 16

How Many Numbers?  Let’s change the program so that the user tells us how many numbers they want to enter… How does the pseudo code change? Start get maxNumbers count = 0 total = 0 while count < maxNumbers get number count = count + 1 total = total + number average = total / maxNumbers display average display total Stop 13 of of 16

How Many Numbers? 14 of of 16

Your Turn: Times Tables  Write a program to display a times table  Display it in the format: 1 x 12 = 12 2 x 12 = 24 etc.  Display “The end” after the times table has been fully shown  Extension: Ask the user which times table to display and how many multiples to go up to  There are at least five trip hazards in there! 15 of of 16

The while loop  We don’t just have to use a simple counter to control our loop, we can use any Boolean expression  Have you ever driven your parents crazy on a car journey asking “When will we get there?”  Try this 16 of of 16

Let’s look more closely… The sentry variable controls access to the loop. It must be initialized. 1.Try this code 2.Try it without the sentry variable initialization 3.Try it without the colon 4.Try it without the tab 5.Try it with a tab before We ask a question and put the response into the sentry variable We check that response does not contain: “We’re here” 17 of of 16

Challenge: Guess My Number  Guess my Number Generate a random number between 1 and 100 Ask the user to guess this number Tell them if their guess is too high or too low Count how many attempts they make before they guess it correctly When they get it right, display a suitable message and tell them how many attempts it took 18 of of 16

Summary  We have looked at loops of increasing complexity  We have related the pseudo code to Python programs  We have learned how to count, total and average using loops  We have learned how to let the user’s input control a loop 19 of of 16