Repetition Structures

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
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.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 7 Problem Solving with Loops
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Chapter Looping 5. The Increment and Decrement Operators 5.1.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Few More Math Operators
Lesson #5 Repetition and Loops.
Week 1, Day 3 Lazy Coding 29 June 2016.
UNIT 5 Lesson 15 Looping.
Chapter 6: Loops.
Repetition Structures
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
ECE Application Programming
Chapter 4 C Program Control Part I
Repetition Structures Chapter 9
Python: Control Structures
Lesson #5 Repetition and Loops.
ECS10 10/10
Loop Structures.
CS1371 Introduction to Computing for Engineers
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
While Loops in Python.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
The while Looping Structure
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
Lesson #5 Repetition and Loops.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Java Programming Loops
Coding Concepts (Basics)
The while Looping Structure
Module 4 Loops.
3.1 Iteration Loops For … To … Next 18/01/2019.
Loop Strategies Repetition Playbook.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
UMBC CMSC 104 – Section 01, Fall 2016
Let’s all Repeat Together
Flowcharts and Pseudo Code
A LESSON IN LOOPING What is a loop?
Java Programming Loops
Lesson #5 Repetition and Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Another Example Problem
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
The while Looping Structure
While Loops in Python.
The while Looping Structure
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Repetition Structures A bit more on…

Practice: Kgs to Lbs Write a program that lists out a conversion table from kilograms to pounds. Include all values from 1 to 100 for both units. Kilograms Pounds Pounds Kilograms 1 2.2 1 0.45 2 4.4 2 0.91 3 6.6 3 1.36 … … 99 217.8 99 44.91 100 220.0 100 45.36

Practice: Heads or Tails Write a program that simulates a coin flip a million times Then, count the # of heads and tails that result from the flips and display your results to the user Also, display the probability of each outcome out of a million tries

Sentinels Sometimes, we will need to enter in a large number of items that need to be calculated in a certain way However, we may not know how many values we will be entering We can do a couple of things here: We can ask the user after every single iteration whether they want to add another value (but this is annoying) We can ask the user ahead of time how many values they’re going to input, but they may not know from the start

Sentinels A sentinel value is a pre-defined value that the user can type in to indicate that they are finished entering data Example: >> enter a test score (type -1 when done): 100 >> enter a test score (type -1 when done): 80 >> enter a test score (type -1 when done): -1 >> your test average was: 90%

Sentinels In this example, “-1” is considered the sentinel. It indicates that the user is finished typing in their data. Sentinels must be distinctive enough that they will not be confused/mistaken as an actual value of data (-1 was a reasonable value in this example because you can’t really get a -1 on a test, unless you’re in Mr. Seok’s class)

Practice – Adding Machine Write a program that asks the user to continually type in integer values Continue to add all the user’s inputted values into a total variable If the user enters “0”, then end the program by displaying the total sum for the user’s values

The “break” Command Some of you may have already used this one, but the word “break” signals to Python to immediately end a loop It will not, however, end your program, it only ends the repetition structure and picks up on the next line of code outside of the repetition structure Note that it will terminate the loop immediately, and it will not run code that follows

The “break” Command x = 0 while x != 10: if x >= 3: break print (x) x + = 1

The While Loop The while loop is sometimes tricky in the sense that you need to set up the condition of the while statement as the negated version of what you’re looking for. For example: x = 0 while x != 10: # you’ll notice here we loop until x = 10 print(x) # the condition is True when x != 10 x += 1

The While Loop The break keyword allows us to reverse the logic back to “normal” in the sense that we can keep a while loop running until a given condition is met by the “if” statement This means we can just begin our while loops with this easy statement: while True:

The “break” Command x = 0 while True: if x == 3: break print (x) x + = 1

The “return” Command We’ve had quite a bit of trouble with this specific keyword. So, I would recommend trying to stay away from it, unless you feel you have a really good understanding of coding, or at least of this specific keyword. It’s worth mentioning that when you use the word “return” inside a loop, which is also inside a function, it will do a similar thing to that of the “break,” but it will stop the process of the function all together, not just the loop.

The “break” Command

The “return” Command

Practice – Prime Numbers Write a program that asks the user to enter any integer Check to see if the integer is prime and print out your result