Loops and Simple Functions COSC 1301. Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Looping Structures: Do Loops
Programming Logic and Design Eighth Edition
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Structured programming
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
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.
Main task -write me a program
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Fundamentals of Python: From First Programs Through Data Structures
More Functions CS303E: Elements of Computers and Programming.
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.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
For loops in programming Assumes you have seen assignment statements and print statements.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
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)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
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.
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 
Programming Fundamentals Enumerations and Functions.
Designing Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
More about Iteration Victor Norman CS104. Reading Quiz.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
REPETITION CONTROL STRUCTURE
Lecture 7: Repeating a Known Number of Times
For loop, definite vs indefinite iteration, range, random
CSC 108H: Introduction to Computer Programming
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
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.
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
Iteration: Beyond the Basic PERFORM
Lesson 06: Functions Class Chat: Attendance: Participation
Looping Topic 4.
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
Functions, Part 1 of 3 Topics Using Predefined Functions
Logical Operators and While Loops
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Building Java Programs
Presentation transcript:

Loops and Simple Functions COSC 1301

Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when the number of times the loop will execute is indefinite General pseudocode for sentinel loop: General pseudocode for sentinel loop: read the first value while value is not the sentinel: process the value process the value read the next value read the next value Watch for infinite loops 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. 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:

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

For Loops: Syntax for in : statementstatement… 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 Loops: Example for i in [1,2,3,4]: print(i) print(i)Output?1234 Example of Sequence format: Begin and end with square brackets Separate values with a comma Called a list

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

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

For Loops: What about range()? range( ) is a function range( ) is a function –Produces a list of numbers –Several variations of accepted

More range() range() stops at: 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 Generalization, check documentation for formal definition

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

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

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

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

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

Functions: Examples We’ve seen: We’ve seen: –main(): We’ve defined main() –range( ): We’ve called range() ; it is built-in –random.randint( ): We’ve called randint() ; it is located in the module random

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

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