Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Python Programming Language
Selection Structures Tonga Institute of Higher Education.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
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.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Making Decisions In Python
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
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,
Geography 465 Assignments, Conditionals, and Loops.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
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.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.
Flow of Control Part 1: Selection
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.
CONTROLLING PROGRAM FLOW
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
Decisions in Python Bools and simple if statements.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
Decision Statements, Short- Circuit Evaluation, Errors.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
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.
 Type Called bool  Bool has only two possible values: True and False.
Java Programming Fifth Edition
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Data Types, Identifiers, and Expressions
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Conditions and Boolean Expressions
Introduction to Decision Structures and Boolean Variables
CS 1111 Introduction to Programming Spring 2019
Topics The if Statement The if-else Statement Comparing Strings
Python Programming Language
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Types, Truth, and Expressions
Presentation transcript:

Decision Making George Mason University

Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python

What is a decision statement? Must always evaluate to true or false The decision must be composed of variables we have already given values to

Two types of decisions Nested decisions remember the results of decisions made before them (in the same nesting) Independent decisions do not

Boolean values and calculations A boolean value must evaluate to true or false Two boolean values can be compared with and or or Use parentheses if you want to combine and and or, i.e. (x and y) or z or x and (y or z), to disambiguate xyx and y False TrueFalse TrueFalse True xyx or y False True FalseTrue

Calculations that evaluate to boolean values ≥ all evaluate to true or false 3 < 2 is false is, is not also evaluate to true or false 3 is 3 is true “jello” is not “blue” is true

Let’s go over examples

if statements in Python if is a keyword; the if statement must end in a colon is is a keyword that compares the variable operation to a string what belongs to a particular if statement is indented

elif statements in Python elif is a keyword; it stands for else if elif is attached to an if or elif before it, and indicates this elif is nested (you cannot have a standalone elif)

if versus elif The one on the left returns 3 The one on the right returns 6 Why? if-elif statements are nested, linked, and mutually exclusive. The plain if statements are not mutually exclusive, don’t know about each other, and all get executed

else statements in Python else is a keyword; it is linked to an if or elif, and gets executed if the if/elif above it is false

else statements in Python else only gets executed if none of the if or elif before it are true

Indentation matters! This is another type of “nesting”, and what people usually refer to as nested if-else statements

boolean calculations in Python 14 Why do we have two ways to compare for equality? y = 5 y is 5 returns true y is 5.0 returns false y == 5 returns true y == 5.0 returns true It is ALWAYS SAFER to use == instead of is, even when comparing things like strings for equality in Python. is compares memory addresses of variables, which gets confusing with strings.

Programming TRAP Assignment Statementx = 5 Boolean Expressionx == 5

boolean types in Python True and False are both keywords and types in Python (capitalization matters) not is a keyword that negates a boolean value the code above returns True

Questions?