CS 115 Lecture Flags.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Sentinel Logic Assumes while loops and input statements.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering Problem Solving and Logic.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
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.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Learning Javascript From Mr Saem
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Making Choices with if Statements
Lecture 6 Repetition Richard Gesick.
Python: Control Structures
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 5: Control Structures II
CS 115 Lecture 8 Structured Programming; for loops
Lecture 07 More Repetition Richard Gesick.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Strings Part 1 Taken from notes by Dr. Neil Moore
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.
CSc 110, Autumn 2016 Lecture 14: Boolean Logic
Lecture 4A Repetition Richard Gesick.
Sometimes true, always true,never true
Logical Operators, Boolean Data Types
More Selections BIS1523 – Lecture 9.
Conditionals & Boolean Expressions
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Programming Misconceptions
CS 115 Lecture Flags.
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
Structured Programming Taken from notes by Dr. Neil Moore
Patterns to KNOW.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Three Special Structures – Case, Do While, and Do Until
Visual Basic – Decision Statements
Algorithm and Ambiguity
Chapter 5: Control Structure
Conditional and iterative statements
If Statements.
A look at Python Programming Language 2018.
© School Improvement Liverpool Limited 2018
Recursion Taken from notes by Dr. Neil Moore
A LESSON IN LOOPING What is a loop?
For loops Taken from notes by Dr. Neil Moore
CHAPTER 4: Conditional Structures
CHAPTER 5: Control Flow Tools (if statement)
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Another Example Problem
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Compare and Order Integers
While Loops in Python.
Lecture 9: Implementing Complex Logic
CprE 185: Intro to Problem Solving (using C)
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

CS 115 Lecture Flags

What is a flag? Think of a mailbox It has a flag to indicate whether there is mail in the box or not The flag does not tell you how much mail you have It doesn’t tell you when the mail was delivered All it states is that “sometime in the past some mail was delivered”

A flag in computer science It’s not new syntax or a special name It’s a variable which can hold two different states (hint: bool!) It holds one value if the “situation happened” and the other value if the “situation did not happen” Things like “the user said they wanted to quit”. “the variable’s value exceeded 500”, “the file had a problem with opening” You could use any data type for the flag, but bools were made specifically for this use

Steps of using a flag Initialize your flag before you use it Should it be set to True? Or to False? It’s up to you. YOU decide what your flag means Give the steps that could cause the flag to change This is most often a loop, but does not have to be. It could be a series of if’s. It’s most often more than a line or two of code. In these steps, if the situation happens, set the flag to the OTHER value. If it started as True, then set it to False and vice versa AFTER the processing is finished, test the flag to see what happened

Example – are there any odd numbers in the data? def main(): # check 10 input numbers to see if any of them were odd odds = False for i in range(10): num = int(input(“Enter a number: “)) if num % 2 == 1: odds = True if odds == True: print(“Yep, at least one odd number”) else: print(“nope, no odds at all”)

Example: are ALL the numbers bigger than 50? def main(): bigger = True for i in range(5): num = int(input(“Enter a number: “)) if num <= 50: bigger = False if bigger == True: print(“yes, all numbers seen were bigger than 50”) else: print(“no, at least one number was smaller than 50”)

Note the patterns “Any” and “All” What they have in common Both do the three steps of using a flag: Initialize, set and test How they differ Whether the initial value of the flag was True (all) or False (any) How the condition was phrased (positively “yes, condition is here!”, or negatively “sorry, one of them failed!”) What the final value of the flag meant: An “any” pattern – the True means “at least one did do it”, False “none of them did” An “all” pattern – the True means “ALL of them did it”, False means “at least one did not”

Mistakes made with flags This one is a very common error In setting the flag, use an if but you should NOT use an else! Like this (the mistake is in red) If num < 0: negflag = True else: negflag = False This kind of logic will always change the flag, one way or the other, every iteration That means that the flag only reflects the very last situation that was tested, it has forgotten its “history”

Incorrect initialization The mistake is initializing the flag to one value, and then setting it to the SAME value in the processing phase good_flag = True if num % 10 == 0: # multiple of 10 #later if good_flag == True: print(“all numbers were multiples of 10!”) else: print(“at least one number was not a multiple of 10”) #dead!! This is NOT the conclusion you can draw from this code!

No initialization! This is a more subtle mistake, sometimes it crashes the program and sometimes it does not def main(): # note, NO initialization of ten_flag for i in range(5): num = int(input(“Enter a number: “)) if num == 10: ten_flag = True if ten_flag == True: print(“at least one ten was seen”) else: print(“no tens seen”) IF there was at least one ten in the input, then the ten_flag is defined by the assignment statement IF there were NO tens in the input, then the flag is never set, and when it is tested at the end, the program crashes with “unknown identifier”