Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
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.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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.
Complexity (Running Time)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
5.05 Apply Looping Structures
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
General Programming Introduction to Computing Science and Programming I.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 - Algorithms and Design
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Programming Logic and Design Fifth Edition, Comprehensive
More Algorithm Design CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
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.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Control Structures RepetitionorIterationorLooping Part I.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Review while loops Control variables Example Infinite loop
Running Totals CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
ITEC 109 Lecture 18 Looping. Review Questions? Conditionals –if / elif / else –and / or / not.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Testing Programs with Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Selection Using IF THEN ELSE CASE Introducing Loops.
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.
Repetitive Structures
REPETITION CONTROL STRUCTURE
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Introduction to Programming
Control Structure Senior Lecturer
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Coding Concepts (Basics)
Chapter 5 Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Looping III (do … while statement)
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Learning Intention I will learn about the standard algorithm for input validation.
Iteration – While Loops
Presentation transcript:

Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1

Validation Loops Prompt user for value while value invalid Basic structure: prompt for initial value of variable while condition which is true if not valid explain error and prompt again code using that variable

Validation in Tax Program Use loops to insure income, dependents non-negative – Note no longer separate branch for valid inputs

Sentinel Loops Goal: Continue some action until user wants to quit Example: Converting Fahrenheit to Celsius until user wants to quit

Sentinel Loops Sentinel = variable that stores “quit”/”continue” value Basic structure: set initial sentinel value to “continue” while sentinel != “continue” value do things inside loop prompt user whether to do loop again set sentinel to “quit” if they don’t

Sentinel Example Sentinel: “yes” or “no” value entered by user Prompt at end of loop: “Do you want to convert another temperature (yes or no)?” – Always tell user what the expected values are! Condition: while sentinel == “yes” Initialization: set sentinel to “yes” before loop – Assumption: user will want to convert at least one temperature

Sentinel Example

Menu Example Often use sentinels in menu driven loops – User repeatedly chooses option from menu – One option is quit – Idea: Use menu choice as sentinel Algorithm: initialize choice to something besides “quit” while choice != quit prompt for choice if/elif’s for non-quit choices else error message if not quit

Menu Example

Counting Inside Loop May need to keep track of how many times loop runs – Example: number of guesses in guessing game Use counter variable to keep track of times – Increment variable each time through loop counter_variable += 1 – Initialize before start of loop – Use counter variable inside/after loop as needed

Guessing Game Example Use counter variable to keep track of times – Call it guesses Increment variable each time through loop – guesses += 1 Initialize before start of loop – Set to 1 since that will be the number of guesses if the user enters the correct number the first time Use counter variable inside/after loop as needed – Print the number of guesses after the loop

Guessing Game Example

Loops for Searching Example: Finding which two integers a square root is between Algorithm: – Prompt user for number and keep squaring increasingly higher integers until find one greater than that number Example: number = 11 – 1 2 < 11 – 2 2 < 11 – 3 2 < 11 – 4 2 >= 17 – “Square root of 11 is between 3 and 4

Search Loop Example

Search Loop Ideas Decide what you are searching for, and how you will know you have found it Use a loop to successively narrow the range where the value can be found Example: How would you guess a number between 1 and 100? – Where would the search start? – How would information that guess too high/too low change the range of the search?