15-110: Principles of Computing

Slides:



Advertisements
Similar presentations
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Advertisements

ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
James Tam Lists In this section of notes you will be introduced to new type of variable that consists of other types.
Review Algorithm Analysis Problem Solving Space Complexity
Computer Science 111 Fundamentals of Programming I Iteration with the for Loop.
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Python Programing: An Introduction to Computer Science
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
More about Iteration Victor Norman CS104. Reading Quiz.
Learning outcomes 5 Developing Code – Using Flowcharts
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: Iterative Statements – Part 1 -> for loop
REPETITION CONTROL STRUCTURE
MSc/ICY Software Workshop , Semester 2
Python Loops and Iteration
Chapter 5: Control Structures II
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Python: Control Structures
Introduction to Computing
CS 115 Lecture 8 Structured Programming; for loops
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Numbering System TODAY AND TOMORROW 11th Edition
Repetition-Counter control Loop
While Loops in Python.
Topics Introduction to Repetition Structures
Structured Programming; for loops Taken from notes by Dr. Neil Moore
ALGORITHMS AND FLOWCHARTS
CHAPTER FOUR Functions.
Determinate Loops with the
Algorithm design and Analysis
Class 14 functions in Python can return multiple values counting loops
CS190/295 Programming in Python for Life Sciences: Lecture 6
ALGORITHMS AND FLOWCHARTS
子夜吳歌 ~ 李白 長安一片月, 萬戶擣衣聲; 秋風吹不盡, 總是玉關情。 何日平胡虜, 良人罷遠征。
Introduction to Algorithms and Programming
Topics Introduction to Repetition Structures
Programming Training Main Points: - Problems with repetitions.
Faculty of Computer Science & Information System
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Suppose I want to add all the even integers from 1 to 100 (inclusive)
子夜吳歌 ~ 李白 長安一片月, 萬戶擣衣聲; 秋風吹不盡, 總是玉關情。 何日平胡虜, 良人罷遠征。
15-110: Principles of Computing
Introduction to Repetition Structures
15-110: Principles of Computing
15-110: Principles of Computing
CISC101 Reminders All assignments are now posted.
CHAPTER 6: Control Flow Tools (for and while loops)
15-110: Principles of Computing
15-110: Principles of Computing
For loops Taken from notes by Dr. Neil Moore
15-110: Principles of Computing
Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations Choose between several.
Chopin’s Nocturne.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
While Loops in Python.
Topic: Iterative Statements – Part 2 -> for loop
Introduction to Python
Presentation transcript:

15-110: Principles of Computing Loop Structures- Part I Lecture 7, September 23, 2018 Mohammad Hammoud Carnegie Mellon University in Qatar

Today… Last Session: Today’s Session: Announcement: Decision Structures- Part II: Multi-way Decisions A Study on Design Today’s Session: Control Structures- Part I: For Loop Examples Announcement: HA2 is due on September 27 by 10:00AM

Towards Loops We looked in detail at the Python if statement and its use in implementing programming patterns such as 1-way, 2-way, and multi- way decisions We will now look at a new structure, known as the control (or loop) structure, which allows us to iterate through a sequence of values We have seen very briefly in the last lecture the Python for statement, which provides an example of a loop

The for Loop A Python for loop has this general form: The <body> of the loop can be any sequence of Python statements <var> is called the loop index, which takes on each successive value in <sequence>, and <body> is executed once for each value The <sequence> portion consists of a list of values E.g., range(n) is a built-in function in Python that generates “on the fly” a sequence of numbers that starts at 0 and ends at n-1 for <var> in <sequence>: <body>

The Flowchart of a for Loop More items in <sequence> No Yes <var> = Next item <body>

Example 1: Average of a Series of Numbers Suppose we want to write a program that can compute the average of a series of numbers entered by the user Here is an algorithm to do so: Input the count of the numbers, n Initialize sum to 0 Loop n times Input a number, x Add x to sum Output average as sum/n

Example 1: Average of a Series of Numbers We can easily translate this algorithm into a Python implementation def main(): n = eval(input("How many numbers do you have? ")) sum = 0.0 for i in range(n): x = eval(input("Enter a number >> ")) sum = sum + x print("\nThe average of the numbers is", sum/n) main()

Example 2: Printing Odd Numbers Suppose we want to write a program that prints odd numbers from 0 to n (inclusive), which can be input by a user Here is how the program can look like: n = eval(input("Enter n: ")) for i in range(n+1): if i % 2 == 1: print(i, end = " ") print()

Example 2: Printing Odd Numbers What if we want to print odd numbers from 1 (NOT 0) to n (inclusive), which can be input by a user? n = eval(input("Enter n: ")) for i in range(n+1): if i == 0: pass else: if i % 2 == 1: print(i, end = " ") print()

Example 2: Printing Odd Numbers What if we want to print odd numbers from 2 (NOT 1) to n (inclusive), which can be input by a user? n = eval(input("Enter n: ")) for i in range(n+1): if i < 2: pass else: if i % 2 == 1: print(i, end = " ") print()

Example 2: Printing Odd Numbers What if we want to print odd numbers from 3 (NOT 2) to n (inclusive), which can be input by a user n = eval(input("Enter n: ")) for i in range(n+1): if i < 3: pass else: if i % 2 == 1: print(i, end = " ") print()

Example 2: Printing Odd Numbers Is there a better way for doing this? Yes, we can use another version of range, namely, range(start, end) s = eval(input("Enter the starting number: ")) e = eval(input("Enter the ending number: ")) for i in range(s, e+1): if i % 2 == 1: print(i, end = " ") print()

Yet, Another Version of Range(.) We can even specify a different increment in the range(…) function via including a third argument to it (i.e., range(start, end, step)) start = eval(input("Enter a starting number: ")) end = eval(input("Enter an ending number: ")) step = eval(input("Enter the step: ")) for i in range(start, end, step): print(i, end = " ") print()

Example 3: Fibonacci Sequence Suppose we want to write a program that computes and outputs the nth Fibonacci number, where n is a value entered by a user The Fibonacci sequence starts with 0 and 1 After these first two numbers, each number in the sequence is computed as simply the sum of the previous two numbers E.g., 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Example 3: Fibonacci Sequence def fibonacci(n): f_i = 0 f_j = 1 print(f_i, f_j, end = " ") for k in range(2, n+1): f_new = f_i + f_j print(f_new, end = " ") f_i = f_j f_j = f_new

Example 3: Fibonacci Sequence n = eval(input("Enter a number that is larger than 1 >> ")) if n < 2: print("You can only enter a number that is larger than 1!") else: fibonacci(n)

Example 4: A Rectangle of Stars How can we write a program that draws the following shape of stars using only 1 for loop and 1 if-else statement? *

Example 4: A Rectangle of Stars How can we write a program that draws the following shape of stars using only 1 for loop and 1 if-else statement? for i in range(9): if i == 0 or i == 8: x = "*********" else: x = "* *" print(x)

Next Lecture… Problem Solving