Repetition Structures

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.
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.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
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”);
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
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.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Repetition Structures The long awaited …. Repetition Structures I repeat …
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
FOP: While Loops.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
UNIT 5 Lesson 15 Looping.
Repetition Structures
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Loops BIS1523 – Lecture 10.
CS161 Introduction to Computer Science
Topics Introduction to Repetition Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
IF statements.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
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: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topics Introduction to Repetition Structures
Web Programming– UFCFB Lecture 16
Control Structures - Repetition
Control Structure Senior Lecturer
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Looping and Repetition
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Loops CIS 40 – Introduction to Programming in Python
Module 4 Loops.
Programming We have seen various examples of programming languages
Introduction to Repetition Structures
Topics Introduction to Repetition Structures
Introduction to Computer Science
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Learning Plan 4 Looping.
Loops CGS3416 Spring 2019 Lecture 7.
Chapter 13 Conditional Repetition
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
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 …

Practice Write a program that a shopping spree! Give the user a welcome statement that lets them know they have $1,000 to spend at the local mall. Then, ask the user for the price of one item at a time until the user runs out of money. You may also want to program the code to update how much money the user has left after each item Finally, print out the total number of items bought with the $1,000

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

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

The “while” Loop

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 Example Equal to += C += 5 C = C + 5 –= C –= 4 C = C – 4 *= C *= 3 C = C * 3 /= C /= 2 C = C / 2 %= C %= 6 C = C % 6

Practice – Christmas Code Rewrite the Christmas code, 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: +, 4 + 3 = ?, 5 + 7 = ?) 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 …