Loops and Simple Functions

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Computer Science 1620 Loops.
Structured programming
Chapter 2 Writing Simple Programs
Main task -write me a program
Fundamentals of Python: From First Programs Through Data Structures
More Functions CS303E: Elements of Computers and Programming.
Python quick start guide
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
For loops in programming Assumes you have seen assignment statements and print statements.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
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.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
More about Iteration Victor Norman CS104. Reading Quiz.
Chapter 2 Writing Simple Programs
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Topics Introduction to Functions Defining and Calling a Void Function
REPETITION CONTROL STRUCTURE
Python: Experiencing IDLE, writing simple programs
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Lecture 7: Repeating a Known Number of Times
Python: Control Structures
For loop, definite vs indefinite iteration, range, random
CS 115 Lecture 8 Structured Programming; for loops
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Functions CIS 40 – Introduction to Programming in Python
Repetition-Counter control Loop
Topics Introduction to Repetition Structures
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Logical Operators and While Loops
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Writing Functions( ) (Part 5)
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Python Primer 2: Functions and Control Flow
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Functions, Part 1 of 3 Topics Using Predefined Functions
Iteration: Beyond the Basic PERFORM
Lesson 06: Functions Class Chat: Attendance: Participation
Topics Introduction to Repetition Structures
Looping Topic 4.
Topics Introduction to Functions Defining and Calling a Void Function
Let’s all Repeat Together
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
Logical Operators and While Loops
For loops Taken from notes by Dr. Neil Moore
COMPUTER PROGRAMMING SKILLS
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Building Java Programs
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Loops and Simple Functions COSC 1301

Review: While Loops Typically used when the number of times the loop will execute is indefinite General pseudocode for sentinel loop: read the first value while value is not the sentinel: process the value read the next value Watch for infinite loops

Exercise Write a program that reads a positive integer from the user, and then prints all the integers from 0 to that integer. Example Execution (user input underlined): Enter a positive integer: 3 1 2 3

More Loops Definite loops for loop Execute the body of the loop a specific number of times Use a counter As opposed to indefinite loops like the while loop

For Loops: Syntax Loop Body Loop Index The Loop Index takes on the values in the sequence, one at a time, and the Loop Body is executed once for each value Indentation Matters! for <variable> in <sequence>: statement …

For Loops: Example for i in [1,2,3,4]: print(i) Output? 1 2 3 4 Loop Index Programmers often use i or j Example of Sequence format: Begin and end with square brackets Separate values with a comma Called a list Can also NOT use the value of i, could just use as a counter (i does not have to appear in the loop body)

For Loops: Example for num in [3,2,1,5]: print(num)

Question: What is the output? for j in [2,4,6,8]: print(j)

For Loops: What about range()? range(<expr>) is a function Produces a list of numbers Several variations of <expr> accepted range(<expr>) Result range(n) A list of integers from 0 to n-1 range(n,m) A list containing the integers from n to m-1 range(n,m,step) A list containing n, n+1*step, n+2*step, n+3*step,… up to m-1 Seen in python tour Show in interpreter example of each: Range(6) Range(2,6) Range(2,6,2)

More range() range() stops at: One less than end if the step is positive One more than end if the step is negative Generalization, check documentation for formal definition

For Loops: Example for i in range(2,6): print (i, i*2)

For Loops: Exercise Write a for loop that prints the even numbers from 2 to 20. Use the % operator

Functions: What are they? Statements grouped under a special name that are executed together Useful to execute statements from different parts of the program Advantages Code reuse Type once, use again and again! Easier to maintain (update, fix mistakes) Code readability

Functions: Syntax def functionName(): statement … Indentation Matters! Function Body

Functions: Syntax Recall: def functionName(): To call that function, type its name: functionName() When a function is called the statements in the body are executed If the function is not called, the statements are never executed

Functions: Examples We’ve seen: main(): We’ve defined main() range(<expr>): We’ve called range(); it is built-in random.randint(<expr>): We’ve called randint(); it is located in the module random Walk through example (draw pictures of function call graph) Run it Discuss debugging techniques

Question: Functions Why are functions useful? A. Improve code readability B. Reduce code maintenance C. Both A and B D. Neither A or B C

More Functions Functions can take one or more parameters print(“Hello World”) Functions may return values results = input(“Enter your name: “)