Sentinel Logic Assumes while loops and input statements.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
CS0004: Introduction to Programming Repetition – Do Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
“Here we go Loop de Loo” (Looping) So far you have learned (I hope) some decision making C++ control structures: *if (condition) else *if (condition) else.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
The Wait-Until-Event pattern Has it happened  Yes, it happened!
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.
For loops in programming Assumes you have seen assignment statements and print statements.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering Problem Solving and Logic.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
Data Validation while loops. Data validation Programs get input data from different sources All data should be ‘validated’ before the program tries to.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
ARITHMETIC OPERATORS COMPARISON/RELATIO NAL OPERATORS LOGIC OPERATORS ()Parenthesis>Greater than &&And ^Exponentiation=>=
Mindstorm NXT-G Introduction Towson University Robotics.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Processing multiple files
Categories of loops definite loop: Executes a known number of times.
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
REPETITION CONTROL STRUCTURE
Lesson 05: Iterations Class Chat: Attendance: Participation
Think What will be the output?
Chapter 5: Repetition Structures
Repetition Structures (Loops)
While Loops Chapter 3.
CS 1428 Exam II Review.
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Logical Operators and While Loops
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
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.
Building Java Programs
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CS 1428 Exam II Review.
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
The switch statement: an alternative to writing a lot of conditionals
Patterns to KNOW.
Building Java Programs
Three Special Structures – Case, Do While, and Do Until
Logical Operators and While Loops
CIS 110: Introduction to Computer Programming
Another Example Problem
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Presentation transcript:

Sentinel Logic Assumes while loops and input statements

Sentinel Logic – uses an indefinite loop The for loop is a definite loop – you know how many times it needs to execute There are many situations in life / programming where you do not know this information (how many times it needs to execute) The while loop (indefinite loop) is made for this kind of situation. Example: “The loop executes until a sensor gives a value out of range” Example: “The user plays a game until they get tired.” Example: “The user inputs a value which must be either Y or N, nothing else.”

A pattern A pattern of statements called “sentinel logic” handles this kind of problem as cleanly as it can be done. A sentinel is an indicator that the loop can stop executing, that the data is used up. It can be a special number (-1, 0, 9999), a string (“stop”), or it can be a condition (keep looping while sensor > 0 and sensor < 50) Pattern Input the first data value to be processed While the data value is not the sentinel Process the data value Input the next data value to be processed