ITEC 109 Lecture 19 For loops. Review While loops –Sentinels –Counter variable –Break –Continue.

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

Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Complexity (Running Time)
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Iterations Very Useful: Ability to repeat a block of code Example:
Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Working with Loops, Conditional Statements, and Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
ITEC 109 Lecture 18 Looping. Review Questions? Conditionals –if / elif / else –and / or / not.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
ITEC 109 Lecture 20 Arrays / Lists. Arrays Review For loops –Rationale –Syntax –Examples.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
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.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
REPETITION CONTROL STRUCTURE
Python Loops and Iteration
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
ECS10 10/10
Chapter 5: Control Structures II
Chapter 5: Loops and Files.
Repetition-Counter control Loop
And now for something completely different . . .
Logical Operators and While Loops
Manipulating Pictures, Arrays, and Loops part 2
Lecture 4A Repetition Richard Gesick.
While Loops.
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Introduction to Programming
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Logical Operators and While Loops
What is x? x = 12 %
Python While Loops.
Presentation transcript:

ITEC 109 Lecture 19 For loops

Review While loops –Sentinels –Counter variable –Break –Continue

For loops Objectives For loops Difference from while loops

For loops Disadvantage s While loops –3 things to worry about –Bugs happen when you are writing code i=0 while (i <10): i=i+1

For loops While 2 types of problems –Numeric control –Alphabetic control i=0; while (i <10): i = i+1; command = raw_input(); while (command != “quit”): command = raw_input();

For loops Rationale Standard problem Problematic practice –Keeping track –Placement issues –Updating issues Need to simplify it

For loops Arrays List of numbers Range takes –Number to start at –Number to end at –Number to increment by (1 by default) Goal: Do something for each value in list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] y = range(0,10) printNow(y)

For loops For All in one place –Initial value –Sentinel check –Increment for i in range(0,10): printNow(i) int i=0; while (i <10): i = i+1;

For loops English Start at the first item in the list, execute the block of code, move to the next value in the list, and repeat until the list runs out for i in range (0,10): #Block of code here

For loops Examples for i in range(0,10): printNow(“SPAM”); sum=0; for i in range(0,5): sum=sum+i; printNow(sum); What does this print?

For loops Pyramid Scheme Get a percentage of profit from those who generate money underneath you purchaseAmount=10; for sucker in range(0,10): val = sucker*(.05*purchaseAmount); printNow(“Amount made with “ + numSuckers + “ underneath you: “ + str(val));

For loops Examples value=0; for num in range(0,40,10): value = value + num; printNow(value); value=10; for (num in range(0,40,10): value = value + num; printNow(value);

For loops Example for num in range(10,0,-1): printNow(num) for num in range(10,-5,-1): printNow(num)

For loops Finding min val=int(raw_input()) min= val for i in range(0,10): val = int(raw_input()) if (min > val): min = val; printNow(min) 1.Assume first entry is what you are looking for 2.Read in another entry 3.Compare against assumption

For loops 2D grid x,y location Need to perform action at each grid location for i in range(0,10): for j in range(0,10): printNow(“At location “ + str(i) + “,” + str(j));

For loops String parsing input = raw_input(); part1=“” part2=“”; found=false; for i in range(0, len(input)): if (input[i] == “:”) found = true; if (not found): part1 = part1 + input[i] else: part2 = part2 + input[i] printNow(part1 + “-” + part2) Ever wonder how index works? This splits a string into two parts Before and after the :

For loops String replace Need to find a particular character and replace it with another character How do we do this without the built-in python function…

For loops Summary For loops –Range - List of numbers –All in one line –Can access variable that tells you where you are at in the list