Repetition Structures The long awaited …. Repetition Structures I repeat …

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
5.05 Apply Looping Structures
CS0007: Introduction to Computer Programming Introduction to Arrays.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
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.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Review while loops Control variables Example Infinite loop
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Chapter 7 Problem Solving with Loops
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
UNIT 5 Lesson 15 Looping.
Repetition Structures
REPETITION CONTROL 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: Looping Starting Out with C++ Early Objects Seventh Edition
Topics Introduction to Repetition Structures
Outline Altering flow of control Boolean expressions
Repetition Structures
Loops CIS 40 – Introduction to Programming in Python
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Repetition Structures The long awaited …

Repetition Structures I repeat …

Repetition Structures So, by now you have come to realize that our codes can become pretty long, especially when we want to perform a task multiple times Let’s take a look at an example from one of your very own …

Repetition Structures You must’ve thought, “There’s gotta be a better way …” and there is! But first, let’s talk about why it SEOKs to use “the old way”: It’s long … (it’s almost like your computer is making you work) More memory, greater space on computer’s hard drive You mess up once, you have to fix it in every repeated step

Repetition Structures The solution is to create a repetition structure, or more commonly called a “loop” This requires two steps: Write the code for the operation you would like to perform Place the code into a special structure that causes Python to repeat it as many times as you would like There are various ways to “loop” in Python, one in which we’ve already seen

The Function Loop By now, you should all know how to loop a function

The Function Loop When we loop functions, we just need to be careful in what exactly we are asking Python to repeat We saw an example where we wanted to keep prompting a user for a guess of the magic number, but we didn’t want to change the number. So, we were able to accomplish this by choosing the randomly generated number outside of the looped function.

The Function Loop Some of you may have noticed that looping functions has it’s drawbacks: Sometimes, it becomes confusing as to which function is being called and which one comes first Local variables are a pain … It can be confusing!

Condition Controlled Loops Today, we are going to learn a new loop, called the “while” loop We call it a “condition-controlled” loop because it is a repetition structure that depends on the fulfillment of a specific condition, similar to the one’s we created in our decision structures In other words, a set of code will be executed for as long as a Boolean expression holds True

The “while” Loop The “while” loop works as follows: Evaluate a Boolean expression If it holds False, skip the block of statements associated with the while loop and continue the program as normal If it holds True: Execute a series of statements At the end of the statement block, re-evaluate the condition of the loop If it is True, repeat If it is False, skip block of statements and continue with program

The “while” Loop

We refer to the process of going through a loop as an “iteration” If a loop cycles 5 times, we say that it has gone through 5 “iterations” The “while” loop is sometimes known as the “pre-test” loop, meaning it only iterates upon the evaluation of True of the Boolean expression This means that you must “set up” your loop before Python will be able to work with it (i.e. setting up a control variable, like “temp”)

WARNING! There is nothing stopping you from writing a while loop for a condition which may never evaluate to False This will cause what we call an “infinite loop” and will never stop re-iterating This is a huge problem because especially when working on our website, the infinite loop may cause the website to freeze The program should end if you press CTRL+C

The Infinite Loop

Accumulator Variables Many programs require you to count or keep track of the total number of iterations that occur We can use an “accumulator variable” to help us do this

Accumulator Variables First, set up the accumulator variable outside of the loop. Generally, they will come right before the repetition structure is entered. Decide on an initial value for your accumulator variable. For most cases, 0 is a good starting place, or maybe even 1. Use a self-referential assignment statement when incrementing an accumulator variable. Example: prize = prize – 10

Accumulator Variables The self-referential assignment statement can be extremely useful and extended to any mathematical operator: A = A + 1 B = B * 2 C = C / 3 D = D – 4

Accumulator Variables However, Python has a shortcut for this They are called “augmented assignment operators”

Augmented Assignment Operators Augmented Assignment Operator ExampleEqual to += C += 5C = C + 5 –= C –= 4C = C – 4 *= C *= 3C = C * 3 /= C /= 2C = C / 2 %= C %= 6C = C % 6

Practice – Trick or TREAT Rewrite the TREAT problem from the Halloween quiz, this time without functions! (let’s see how much shorter we can make this …) Recall: the user has an opportunity to guess a number between 1 and 10 and if they are right, they win $100. If they are wrong, they get to guess again but their prize money goes down by $10 every time they guess. The user should always win at least $10!

Practice – IXL!!!!! Since you all loved IXL soooo much, we are going to recreate the program. Using a single operator, generate random numbers to create practice problems for the given operator. (ex: operator: +, = ?, = ?) Each time, the user gets a problem right, they are awarded a point, and you will print out the number they have gotten right/wrong after each problem.

Practice – Elementary School Tool Write a program that prints out every factor of a given number. Don’t you wish you had this in elementary school …