Computer Science 111 Fundamentals of Programming I Iteration with the for Loop.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Chapter 2: Algorithm Discovery and Design
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
CS150 Introduction to Computer Science 1
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Copyright © Texas Education Agency, Computer Programming For Loops.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
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.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Control Structures FOR Statement Looping.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Counting Loops.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
More about Iteration Victor Norman CS104. Reading Quiz.
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
Python Programming: An Introduction to Computer Science
Python: Control Structures
Python Programming: An Introduction to Computer Science
Think What will be the output?
While Loops in Python.
Computer Science 101 While Statement.
Lecture 4A Repetition Richard Gesick.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
Introduction to pseudocode
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
Chapter 6: Repetition Statements
For Loops (Iteration 1) Programming Guides.
15-110: Principles of Computing
CHAPTER 6: Control Flow Tools (for and while loops)
Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations Choose between several.
Chopin’s Nocturne.
Python While Loops.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
While Loops in Python.
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Fundamentals of Python: First Programs Second Edition
Presentation transcript:

Computer Science 111 Fundamentals of Programming I Iteration with the for Loop

Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations –Choose between several alternative sequences (selection) –Repeat a single sequence (iteration)

Doing the Same Thing Many Times It’s possible do do something repeatedly by just writing it all out Print ‘hello’ 5 times Statements Count n times >>> print('Hello!') Hello >>> print('Hello!') Hello >>> print('Hello!') Hello >>> print('Hello!') Hello >>> print('Hello!') Hello

Iteration and Loops A loop repeats a sequence of statements A definite loop repeats a sequence of statements a predictable number of times Statements Count n times >>> for x in range(5): print('Hello!')... Hello

The for Loop Statements Count n times Python’s for loop can be used to iterate a definite number of times >>> for x in range(5): print('Hello!')... Hello! for in range( ): Use this syntax when you have only one statement to repeat

The for Loop Statements Count n times >>> for x in range(3):... print('Hello!')... print('goodbye')... Hello! goodbye Hello! goodbye Hello! goodbye for in range( ): … Use indentation to format two or more statements below the loop header loop header loop body

Using the Loop Variable Statements Count n times >>> for x in range(5): print(x) >>> list(range(5)) # Show as a list [0, 1, 2, 3, 4] The loop variable picks up the next value in a sequence on each pass through the loop The expression range(n) generates a sequence of int s from 0 through n - 1 loop variable

Counting from 1 through n Statements Count h - l times >>> for x in range(1, 6): print(x) >>> list(range(1, 6)) # Show as a list [1, 2, 3, 4, 5] The expression range(low, high) generates a sequence of int s from low through high - 1

Skipping Steps in a Sequence Statements >>> for x in range(1, 6, 2): print(x) >>> list(range(1, 6, 2)) # Show as a list [1, 3, 5] The expression range(low, high, step) generates a sequence of int s starting with low and counting by step until high - 1 is reached or exceeded Count (h - l + 1)/s times

Accumulator Loop: Summation sum = 0 for n in range(1, 6): sum = sum + n print(sum) Compute and print the sum of the numbers between 1 and 5, inclusive In this program, the variable sum is called an accumulator variable

Accumulator Loop: Product product = 1 for n in range(1, 6): product = product * n print(product) Compute and print the product of the numbers between 1 and 5, inclusive The loop pattern or idiom is the same as the sum loop Vary the initial value of the accumulator variable and the means of increasing it

Using a Loop in a Real Problem An investor deposits $10,000 with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) of 6% for a period of 5 years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal.

Design in Pseudocode principal = rate =.06 term = 5 totalinterest = 0 for each year in term print principal interest = principal * rate print interest principal = principal + interest totalinterest = totalinterest + interest print totalinterest print principal An investor deposits $10,000 with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) of 6% for a period of 5 years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal.

An investor deposits $10,000 with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) of 5% for a period of 5 years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal. principal = rate =.06 term = 5 totalinterest = 0 for year in range(term): print(principal) interest = principal * rate print(interest) principal = principal + interest totalinterest = totalinterest + interest print(totalinterest) print(principal) First Coding in Python

Refinement: Clean Up Output An investor deposits $10,000 with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) of 5% for a period of 5 years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal. principal = rate =.06 term = 5 totalinterest = 0 for year in range(term): interest = principal * rate print(principal, interest) principal = principal + interest totalinterest = totalinterest + interest print('Total interest:', totalinterest) print('Total principal:', principal)

Second Refinement: $d.cc An investor deposits $10,000 with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) of 5% for a period of 5 years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal. principal = rate =.06 term = 5 totalinterest = 0 for year in range(term): interest = principal * rate print(round(principal, 2), round(interest, 2)) principal = principal + interest totalinterest = totalinterest + interest print('Total interest:', round(totalinterest, 2)) print('Total principal:', round(principal, 2)

Third Refinement: Print the Year An investor deposits $10,000 with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) of 5% for a period of 5 years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal. principal = rate =.06 term = 5 totalinterest = 0 for year in range(1, term + 1): interest = principal * rate print(year, round(principal, 2), round(interest, 2)) principal = principal + interest totalinterest = totalinterest + interest print('Total interest:', round(totalinterest, 2)) print('Total principal:', round(principal, 2))

Generalize to Solve for Any Principal, Rate, and Term An investor deposits a given amount with the Get-Rich-Quick agency and receives a statement predicting the earnings on an annual percentage rate (APR) for a period of years. Write a program that prints the beginning principal and the interest earned for each year of the period. The program also prints the total amount earned and the final principal. principal = ? rate = ? term = ? totalinterest = 0 for year in range(1, term + 1): interest = principal * rate print(year, round(principal, 2), round(interest, 2)) principal = principal + interest totalinterest = totalinterest + interest print('Total interest:', round(totalinterest, 2)) print('Total principal:', round(principal, 2))

For Tuesday/Wednesday Read Section 3.2 on Formatting Output Read Section 3.4 on Selection Statements