Repetition Structures

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
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.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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.
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.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
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.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
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.
Repetition Structures The long awaited …. Repetition Structures I repeat …
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.
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.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
ECE Application Programming
Formatting Output.
Python: Control Structures
Lesson #5 Repetition and Loops.
ECS10 10/10
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
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
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
Writing Functions( ) (Part 5)
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.
Chapter 4 Control structures and Loops
Building Java Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lesson #5 Repetition and Loops.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Repetition Structures
Java Programming Loops
Do While (condition is true) … Loop
Repetition Structures
Module 4 Loops.
Chapter 5 Loops.
Building Java Programs
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
3.1 Iteration Loops For … To … Next 18/01/2019.
Loop Strategies Repetition Playbook.
Python programming exercise
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
Loops.
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 – 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! Only write up the part of the code where the user can keep guessing until they win.

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 – IXL Extension Extend your IXL program so that if the user answers 5 problems correctly in a row, they are considered a “math master!” Print out something that says “You are now a MATH MASTER of …” whatever operator you were testing 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 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