Topics Introduction to Repetition Structures

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 5: Loops and Files.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Topics Introduction Hardware and Software How Computers Store Data
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Sixth Edition Chapter 5 Looping.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
CS 100 Introduction to Computing Seminar October 7, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
CSC 1010 Programming for All Lecture 2 Introduction to Python Some material based on material from Marty Stepp, Instructor, University of Washington.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
REPETITION CONTROL STRUCTURE
Topics Introduction Hardware and Software How Computers Store Data
CHAPTER 5A Loop Structure
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topics Introduction to File Input and Output
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4: Loops and Files
Topics Introduction Hardware and Software How Computers Store Data
Loops CIS 40 – Introduction to Programming in Python
Topics Introduction to Repetition Structures
Chapter 6: Repetition Statements
Algorithm and Ambiguity
Topics Sequences Introduction to Lists List Slicing
Topics Designing a Program Input, Processing, and Output
Topics Augmented Assignment Operators Sentinels Input Validation Loops
Introduction to Repetition Structures
Using the Target Variable Inside the Loop
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Introduction to File Input and Output
Topics Introduction to Repetition Structures
Topics Sequences Introduction to Lists List Slicing
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Topics Introduction to File Input and Output
Presentation transcript:

Topics Introduction to Repetition Structures The while Loop: a Condition-Controlled Loop The for Loop: a Count-Controlled Loop Calculating a Running Total Sentinels Input Validation Loops Nested Loops

Introduction to Repetition Structures Often have to write code that performs the same task multiple times Disadvantages to duplicating code Makes program large Time consuming May need to be corrected in many places Repetition structure: makes computer repeat included code as necessary Includes condition-controlled loops and count-controlled loops

The while Loop: a Condition-Controlled Loop while loop: while condition is true, do something Two parts: Condition tested for true or false value Statements repeated as long as condition is true In flow chart, line goes back to previous part General format: while condition: statements

The while Loop: a Condition-Controlled Loop (cont’d.)

The while Loop: a Condition-Controlled Loop (cont’d.) In order for a loop to stop executing, something has to happen inside the loop to make the condition false Iteration: one execution of the body of a loop while loop is known as a pretest loop Tests condition before performing an iteration Will never execute if condition is false to start with Requires performing some steps prior to the loop

Infinite Loops Loops must contain within themselves a way to terminate Something inside a while loop must eventually make the condition false Infinite loop: loop that does not have a way of stopping Repeats until program is interrupted Occurs when programmer forgets to include stopping code in the loop

The for Loop: a Count-Controlled Loop Count-Controlled loop: iterates a specific number of times Use a for statement to write count-controlled loop Designed to work with sequence of data items Iterates once for each item in the sequence General format: for variable in [val1, val2, etc]: statements Target variable: the variable which is the target of the assignment at the beginning of each iteration

Using the range Function with the for Loop The range function simplifies the process of writing a for loop range returns an iterable object Iterable: contains a sequence of values that can be iterated over range characteristics: One argument: used as ending limit Two arguments: starting value and ending limit Three arguments: third argument is step value

Using the Target Variable Inside the Loop Purpose of target variable is to reference each item in a sequence as the loop iterates Target variable can be used in calculations or tasks in the body of the loop Example: calculate square root of each number in a range

Letting the User Control the Loop Iterations Sometimes the programmer does not know exactly how many times the loop will execute Can receive range inputs from the user, place them in variables, and call the range function in the for clause using these variables Be sure to consider the end cases: range does not include the ending limit

Generating an Iterable Sequence that Ranges from Highest to Lowest The range function can be used to generate a sequence with numbers in descending order Make sure starting number is larger than end limit, and step value is negative Example: range (10, 0, -1)

Calculating a Running Total Programs often need to calculate a total of a series of numbers Typically include two elements: A loop that reads each number in series An accumulator variable Known as program that keeps a running total: accumulates total and reads in series At end of loop, accumulator will reference the total

Calculating a Running Total (cont’d.)

The Augmented Assignment Operators In many assignment statements, the variable on the left side of the = operator also appears on the right side of the = operator Augmented assignment operators: special set of operators designed for this type of job Shorthand operators

The Augmented Assignment Operators (cont’d.)

Sentinels Sentinel: special value that marks the end of a sequence of items When program reaches a sentinel, it knows that the end of the sequence of items was reached, and the loop terminates Must be distinctive enough so as not to be mistaken for a regular value in the sequence Example: when reading an input file, empty line can be used as a sentinel

Input Validation Loops Computer cannot tell the difference between good data and bad data If user provides bad input, program will produce bad output GIGO: garbage in, garbage out It is important to design program such that bad input is never accepted

Input Validation Loops (cont’d.) Input validation: inspecting input before it is processed by the program If input is invalid, prompt user to enter correct data Commonly accomplished using a while loop which repeats as long as the input is bad If input is bad, display error message and receive another set of data If input is good, continue to process the input

Input Validation Loops (cont’d.)

Nested Loops Nested loop: loop that is contained inside another loop Example: analog clock works like a nested loop Hours hand moves once for every twelve movements of the minutes hand: for each iteration of the “hours,” do twelve iterations of “minutes” Seconds hand moves 60 times for each movement of the minutes hand: for each iteration of “minutes,” do 60 iterations of “seconds”

Nested Loops (cont’d.) Key points about nested loops: Inner loop goes through all of its iterations for each iteration of outer loop Inner loops complete their iterations faster than outer loops Total number of iterations in nested loop: number_iterations_inner x number_iterations_outer

Summary This chapter covered: Repetition structures, including: Condition-controlled loops Count-controlled loops Nested loops Infinite loops and how they can be avoided range function as used in for loops Calculating a running total and augmented assignment operators Use of sentinels to terminate loops

Interactive Mode When you start Python in interactive mode, you will see a prompt Indicates the interpreter is waiting for a Python statement to be typed Prompt reappears after previous statement is executed Error message displayed If you incorrectly type a statement Good way to learn new parts of Python

Writing Python Programs and Running Them in Script Mode Statements entered in interactive mode are not saved as a program To have a program use script mode Save a set of Python statements in a file The filename should have the .py extension To run the file, or script, type python filename at the operating system command line

The IDLE Programming Environment IDLE (Integrated Development Program): single program that provides tools to write, execute and test a program Automatically installed when Python language is installed Runs in interactive mode Has built-in text editor with features designed to help write Python programs

Summary This chapter covered: Main hardware components of the computer Types of software How data is stored in a computer Basic CPU operations and machine language Fetch-decode-execute cycle Complex languages and their translation to machine code Installing Python and the Python interpreter modes