Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
1 MATERI PENDUKUNG JUMP Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Chapter 4: Control Structures II
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 2 Control.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
COMP Loop Statements Yi Hong May 21, 2015.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 6: Loops.
Lecture 6 Repetition Richard Gesick.
Loops in Java.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
The University of Texas – Pan American
Chapter (3) - Looping Questions.
IST256 : Applications Programming for Information Systems
Intro to Computer Science CS1510 Dr. Sarah Diesburg
© 2016 Pearson Education, Ltd. All rights reserved.
Types, Truth, and Expressions (Part 2)
Introduction to Repetition Structures
Understanding Conditions
The structure of programming
Presentation transcript:

Chapter 2 Control

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repeating statements Besides selecting which statements to execute, a fundamental need in a program is repetition –repeat a set of statements under some conditions With both selection and repetition, we have the two most necessary programming statements

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. While and For statements The while statement is the more general repetition construct. It repeats a set of statements while some condition is True. The for statement is useful for iteration, moving through all the elements of data structure, one at a time.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. while loop Top-tested loop (pretest) –test the boolean before running –test the boolean before each iteration of the loop while boolean expression: suite

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. repeat while the boolean is true while loop will repeat the statements in the suite while the boolean is True (or its Python equivalent) If the Boolean expression never changes during the course of the loop, the loop will continue forever.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 2.8

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. General approach to a while outside the loop, initialize the boolean somewhere inside the loop you perform some operation which changes the state of the program, eventually leading to a False boolean and exiting the loop Have to have both!

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. for and iteration One of Python's strength's is it's rich set of built-in data structures The for statement iterates through each element of a collection (list, etc.) for element in collection: suite

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. More Control: Repetition

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Developing a while loop Working with the loop control variable: Initialize the variable, typically outside of the loop and before the loop begins. The condition statement of the while loop involves a Boolean using the variable. Modify the value of the control variable during the course of the loop

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Issues: Loop never starts: the control variable is not initialized as you thought (or perhaps you don't always want it to start) Loop never ends: the control variable is not modified during the loop (or not modified in a way to make the Boolean come out False )

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. while loop, round two while loop, oddly, can have an associated else suite else suite is executed when the loop finishes under normal conditions –basically the last thing the loop does as it exits

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. while with else while booleanExpression: suite else: suite rest of the program

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Break statement A break statement in a loop, if executed, exits the loop It exists immediately, skipping whatever remains of the loop as well as the else statement (if it exists) of the loop

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 2.20 Loop, Hi Lo Game

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Continue statement A continue statement, if executed in a loop, means to immediately jump back to the top of the loop and re-evaluate the conditional Any remaining parts of the loop are skipped for the one iteration when the continue was executed

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. change in control: Break and Continue while loops are easiest read when the conditions of exit are clear Excessive use of continue and break within a loop suite make it more difficult to decide when the loop will exit and what parts of the suite will be executed each loop. Use them judiciously.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. While overview while test1: statement_list_1 if test2: break # Exit loop now; skip else if test3: continue # Go to top of loop now # more statements else: statement_list_2 # If we didn't hit a 'break' # 'break' or 'continue' lines can appear anywhere

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Range and for loop

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Range function The range function represents a sequence of integers the range function takes 3 arguments: –the beginning of the range. Assumed to be 0 if not provided –the end of the range, but not inclusive (up to but not including the number). Required –the step of the range. Assumed to be 1 if not provided if only one arg provided, assumed to be the end value

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Iterating through the sequence for num in range(1,5): print(num) range represents the sequence 1, 2, 3, 4 for loop assigns num to each of the values in the sequence, one at a time, in sequence prints each number (one number per line)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. for num in range(1,5): print(num) Execution of line “ for num in range(1,5):” the first time it is met: –A range object is created. It represents the sequence 1, 2, 3, 4 –assigns num to the first values in the sequence, that is num = 1. The body/suite of the statement is executed one at a time, in sequence –First num becomes 1 and the suite is executed (in this example the print statement) prints each number (one number per line)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. range generates on demand Range generates its values on demand