For loops in programming Assumes you have seen assignment statements and print statements.

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

While loops.
Introduction to Computing Science and Programming I
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Sentinel Logic Assumes while loops and input statements.
Simple Python Loops Sec 9-7 Web Design.
Fundamentals of Python: From First Programs Through Data Structures
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Fundamentals of Python: First Programs
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CPS120 Introduction to Computer Science Iteration (Looping)
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
If statements while loop for loop
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Visual Basic Programming
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Game Programming Step-01 how to create a camera to render the game
CPS120 Introduction to Computer Science Iteration (Looping)
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
Introduction to Computers and Programming Lecture 7:
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
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.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
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.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
More about Iteration Victor Norman CS104. Reading Quiz.
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.
Repetition: the for loop
While Loops in Python.
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
One-Dimensional Array Introduction Lesson xx
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Loops.
CHAPTER 6: Control Flow Tools (for and while loops)
For loops Taken from notes by Dr. Neil Moore
Loops and Simple Functions
Repetition: the for loop
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
While Loops in Python.
Topic: Iterative Statements – Part 2 -> for loop
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Introduction to Python
Presentation transcript:

For loops in programming Assumes you have seen assignment statements and print statements

Why do we want a loop in a program? Many programs need to process huge amounts of data Each piece of data needs to go through the same instructions One option is to write out the instructions over and over again That’s not very practical or flexible What if I wrote a program to process invoices for 100 customers and I got some more customers? Rewrite the program?? No! A loop lets me specify what needs to be repeated and how many times it needs to be repeated

Two kinds of loops Definite You know that you want to execute the loop a certain number of times “there are 10 students’ grades” “Draw a standard chess board” (always 8 x 8 squares) “Quiz the user about the capitals of the 50 states” “do this for every character in the string” “do this for every item in the list” “do this for every line in the file” Indefinite You don’t know how many times you want to execute the loop but you know there is a condition that is the stopping point “do this until the user wants to stop” “let the user enter numbers until they type -1” “do this until the user wins the game” “repeat this until this number reaches a certain level”

For loop – a definite loop The syntax of the for loop statement for variable in some list of values to assign to the variable : (colon!) Follow this line by all statements that should be repeated with an extra indent - This is how the interpreter knows what is in the loop and what’s not The semantics of the for loop statement At the start of the loop check to see if the loop should execute If not, skip the body and continue with next statement after loop If it should execute, Assign variable with the first item in the list Execute the body Repeat the test at the top of the loop to see if loop should execute again

Controlling the loop You don’t want a loop that doesn’t stop! Python uses several ways to control a loop The range function (gives integers) The contents of a list of values (gives the elements whatever type) The contents of a string (gives characters) The contents of a file (gives strings)