CS 115 Lecture Flags.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Loops – While, Do, For Repetition Statements Introduction to Arrays
The If/Else Statement, Boolean Flags, and Menus Page 180
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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?
CPS120 Introduction to Computer Science Iteration (Looping)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
CPS120 Introduction to Computer Science Iteration (Looping)
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
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.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
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.
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.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
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
Lecture 4b Repeating With Loops
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Making Choices with if Statements
CS 115 Lecture Flags.
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
Python: Control Structures
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 5: Control Structures II
CS 115 Lecture 8 Structured Programming; for loops
JavaScript: Control Statements.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Strings Part 1 Taken from notes by Dr. Neil Moore
Control Structures.
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.
Lecture 4A Repetition Richard Gesick.
Sometimes true, always true,never true
More Selections BIS1523 – Lecture 9.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Programming Misconceptions
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
Topic 1: Problem Solving
Structured Programming Taken from notes by Dr. Neil Moore
Coding Concepts (Basics)
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
Computing Fundamentals
If Statements.
Recursion Taken from notes by Dr. Neil Moore
Flowcharts and Pseudo Code
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
Review of Previous Lesson
While Loops in Python.
Lecture 9: Implementing Complex Logic
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 keyword 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

Naming a flag variable Of course it can be any valid identifier but… Try to make the name express the concept the flag represents It’s usually in the form of a question (but don’t try to use a ?) saw_error would make sense to be True if at least one error was seen and False otherwise any_vowels would be True if at least one vowel was seen, False otherwise in_order would be True if all the data seen was in order and False otherwise

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 almost always an if. It could be inside a loop or just a series of if’s. In these steps, if the situation happens, set the flag to the OTHER value (not the one it was initialized to). 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 (usually an if/else structure)

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 the most 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”

Flag Error: 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 in the program if good_flag == True: #Always True! print(“all numbers were multiples of 10!”) else: print(“at least one number was not a multiple of 10”) #dead!!

Flag Error: 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 10 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”