Decision Making CMSC 201 Chang (rev. 2015-02-05).

Slides:



Advertisements
Similar presentations
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Advertisements

James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
If Statements & Relational Operators Programming.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Making Decisions In Python
The If/Else Statement, Boolean Flags, and Menus Page 180
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
While Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Variables and Expressions CMSC 201 Chang (rev )
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
CPS120: Introduction to Computer Science Decision Making in Programs.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops For Loops.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
 Type Called bool  Bool has only two possible values: True and False.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
The switch Statement, and Introduction to Looping
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Chapter 4: Making Decisions.
Python: Control Structures
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
3. Decision Structures Rocky K. C. Chang 19 September 2018
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Conditional and iterative statements
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Topics The if Statement The if-else Statement Comparing Strings
Class 13 function example unstring if if else if elif else
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CHAPTER 5: Control Flow Tools (if statement)
Presentation transcript:

Decision Making CMSC 201 Chang (rev )

Overview Today we will learn about: Boolean expressions Decision making

Boolean Expressions Boolean expressions evaluate to True or False. Boolean variables have their own operators. a = True b = False c = (10 > 4) d = (someVar == someOtherVar)

Boolean Math Operators We can use the following mathematical operators when constructing boolean expressions:

Example a = 4 b = 5 c = 3 bool1 = ( a == b ) bool2 = ( c < b ) bool3 = ( c != a ) print(bool1, bool2, bool3) Prints: False True True

Boolean Logic Boolean operators and or not combine boolean values

and bool1 = ( a and b ) Value of aValue of bValue of bool1 True False TrueFalse For (a and b) to be true, both a and b must be true.

or bool1 = ( a or b ) Value of aValue of bValue of bool1 True FalseTrue FalseTrue False For (a or b) to be true, at least one of a and b must be true.

not bool1 = (not a) Value of aValue of bool1 TrueFalse True not a is the opposite of a

Complex Expressions bool1 = a and (b or c) Value of aValue of bValue of cValue of bool1 True FalseTrue FalseTrue False True False TrueFalse TrueFalse

Short Circuit Evaluation Notice that in the expression: bool1 = a and (b or c) If a is False, the whole expression is False and does not depend on the value of (b or c). Python will realize this, and if a is False and will not evaluate (b or c). Try: bool1 = a and print("Hello")

Practice a = 4 b = 5 c = 6 d = True e = False bool1 = d and (a > b) bool2 = (not d) or (b != c) bool3 = (d and (not e)) or (a > b) bool4 = (a % b == 2) and ((not d) or e)

Numbers and Booleans What about this? a = 4 b = True c = a and b print(c) Prints: True

Numbers and Booleans Python accepts anything that is non-zero as True (there are some exceptions, but we’ll get into those later). So technically you can use any integer as a boolean expression.

Decision Making Why do we care so much about booleans?

If Statements An if statement only executes if a given boolean expression evaluates to True. if booleanExpression: line-1 line-2 line-3 line-4 Anything indented in after the if statement executes if and only if booleanExpression == True

If Statements line-1 if booleanExpression: line-2 line-3 line-4 line-5 This code would produce the following flowchart structure: line-1 Condition is true line-2 line-3 Condition is false line-4 line-5

Example number = int(input("Enter a number ")) if number > 0: print("You entered a positive number") print("This part always execute")

Vocab A block is an indented section of your code. A conditional is the boolean expression in an if statement. If statements are a type of control structure, since it controls the flow of your code.

Nested If Statements We can also "nest" if statements. line-1 if someCondition: if somethingElse: line-2 else: line-3 else: line-4

Exercise Write a code snippet that asks for two numbers for the user. If they are equal, it should print out "Equal", if the first is greater than the second, it should print out "Greater", and if the second is greater than the first it should print out "Less than"

Exercise a = int(input("Enter a number: ")) b = int(input("Enter another number: ")) if a == b: print("Equal") if a > b: print("Greater") if a < b: print("Less than")

Else a = int(input("Enter a number: ")) if a > 0: print("a is greater than zero!") if a <= 0: print("a is less than or equal to zero!") This pattern, where you have an if statement, followed by an if statement that is the complete opposite, happens so often it has a special keyword.

Else a = int(input("Enter a number: ")) if a > 0: print("a is greater than zero!") else: print("a is less than or equal to zero!") The "else" keyword says that if the first if statement doesn’t execute, the else will.

Else line-1 if someBoolean: line-2 line-3 else: line-4 line-5 line-6 line-1 Condition is true line-2 line-3 Condition is false line-4 line-5 Line-6

Elif Cascading if statements: if a > 0: print("A is positive") else: if a < 0: print("A is negative") Elif lets us combine that if and that else.

Elif if a > 0: print("A is positive") elif a < 0: print("A is negative") Now the elif statement will only execute if: The first statement DOES NOT execute, and a < 0

Elif line-1 if someBoolean: line-2 line-3 elif someOtherBoolean: line-4 line-5 line-6 line-1 someBoolean is true line-2 line-3 someBoolean is false AND someOtherBoolean is true line-4 line-5 Line-6 someBoolean is false AND someOtherBoolean is false

Exercise Request an input from the user. If it’s positive, print out the square root. If it’s negative, print out whether it’s even or odd.

Exercise inputNum = int(input("Enter a number")) if inputNum < 0: if inputNum % 2 == 0: print("Number is even") else: print("Number is odd") elif inputNum > 0: print(inputNum ** 0.5)