Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Python Programming Language
CS0007: Introduction to Computer Programming
Conditional Statements Introduction to Computing Science and Programming I.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Boolean expressions Conditional statements and expressions.
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
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
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.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
CSC 107 – Programming For Science. Announcements.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Agenda Basic Logic Purpose if statement if / else statement
Decision Structures, String Comparison, Nested Structures
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
5.04 Apply Decision Making Structures
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
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.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 Type Called bool  Bool has only two possible values: True and False.
Introduction to Decision Structures and Boolean Variables
CS0007: Introduction to Computer Programming
Chapter 6 Conditionals.
Python: Control Structures
IF statements.
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Introduction To Robot Decision Making
Decision Structures, String Comparison, Nested Structures
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Decision Structures, String Comparison, Nested Structures
Decision Structures, String Comparison, Nested Structures
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Introduction To Robot Decision Making
Topics The if Statement The if-else Statement Comparing Strings
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
PYTHON: BUILDING BLOCKS Sequencing & Selection
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Understanding Conditions
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Conditionals.
Chapter 6 Conditionals.
Presentation transcript:

Decision Structures and Boolean Variables

Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming “sequence structures” That just means that the program will execute the statements in the order in which they appear in the source code That just means that the program will execute the statements in the order in which they appear in the source code We would now like for programs to deviate from the linear structure to adapt according to conditions being met, or not met We would now like for programs to deviate from the linear structure to adapt according to conditions being met, or not met

Calculating Overtime For instance, if an employee works more than 40 hours in a week, he/she is entitled to overtime pay For instance, if an employee works more than 40 hours in a week, he/she is entitled to overtime pay Overtime pay is calculated at the rate of 1.5 times the normal hourly rate Overtime pay is calculated at the rate of 1.5 times the normal hourly rate The additional rate is only applied to the hours worked above 40 hours The additional rate is only applied to the hours worked above 40 hours

Calculating Overtime Input: hourly rate of pay Input: hourly rate of pay Input: number of hours worked in a week Input: number of hours worked in a week If hours worked is less than 40, simply multiply hours worked by hourly rate If hours worked is less than 40, simply multiply hours worked by hourly rate If hours worked is greater than 40: If hours worked is greater than 40: Multiply hourly rate by 40 Multiply hourly rate by 40 Subtract 40 from hours worked Subtract 40 from hours worked Multiply overtime hours by 1.5 times hourly rate Multiply overtime hours by 1.5 times hourly rate Add overtime pay to base pay Add overtime pay to base pay

Calculating Overtime Currently, we don’t know enough on Python to deviate from a linear sequence structure Currently, we don’t know enough on Python to deviate from a linear sequence structure We cannot account for when the user inputs a total amount of hours worked that is above 40 to pay for overtime We cannot account for when the user inputs a total amount of hours worked that is above 40 to pay for overtime

The Selection Statement Allows program to “ask a question” and respond accordingly Allows program to “ask a question” and respond accordingly Simplest form: perform an action only if a certain condition is met Simplest form: perform an action only if a certain condition is met If the condition is not met, then the action is not performed (we will learn how to accommodate for non-met conditions with a separate action later on) If the condition is not met, then the action is not performed (we will learn how to accommodate for non-met conditions with a separate action later on)

The Selection Statement In this program, we start by asking a question, “is it cold outside?” In this program, we start by asking a question, “is it cold outside?” If the answer is yes (aka “True”) then we execute an alternate set of commands If the answer is yes (aka “True”) then we execute an alternate set of commands Otherwise, we continue with the program as-is Otherwise, we continue with the program as-is

The Selection Statement Python will read your inputs of “yes” and “no” as holding the values of “True” or “False” Python will read your inputs of “yes” and “no” as holding the values of “True” or “False”

The Selection Statement In Python, we use the keyword “if” to start a selection statement In Python, we use the keyword “if” to start a selection statement We must also use colons to end the selection statement We must also use colons to end the selection statement The block of code reserved to execute if and only if the condition is met, must be indented within the selection statement (nested structure) The block of code reserved to execute if and only if the condition is met, must be indented within the selection statement (nested structure)

Boolean Expressions WRITING A CONDITION

Writing a condition Python doesn’t read the way you would, so you can’t write your conditions as questions like, “Is it cold outside?” Python doesn’t read the way you would, so you can’t write your conditions as questions like, “Is it cold outside?” The key is to write a selection statement in a way Python might understand and that matches the question you are trying to ask The key is to write a selection statement in a way Python might understand and that matches the question you are trying to ask All selection statements must have a condition to be “tested” as either True or False All selection statements must have a condition to be “tested” as either True or False Think about asking questions that can be answered using “yes” or “no” Think about asking questions that can be answered using “yes” or “no”

Boolean Expressions

Named after George Boole, a 19 th century English philosopher and mathematician Named after George Boole, a 19 th century English philosopher and mathematician Boole developed a system of mathematics that allows us to work with the concepts of “True” or “False” Boole developed a system of mathematics that allows us to work with the concepts of “True” or “False” Boole is considered one of the founders of modern computer science, as his work is reflected in the way computers process binary data Boole is considered one of the founders of modern computer science, as his work is reflected in the way computers process binary data

Boolean Expressions Boolean expressions can be used as the condition of an “if” selection statement Boolean expressions can be used as the condition of an “if” selection statement They are generally formed using “relational operators” which allow you to test whether a specific relationship exists between two (or more) values They are generally formed using “relational operators” which allow you to test whether a specific relationship exists between two (or more) values

Relational Operators A > B # A is greater than B A > B # A is greater than B A < B # A is less than B A < B # A is less than B A == B # A is equal to B A == B # A is equal to B A >= B # A is greater than OR equal to B A >= B # A is greater than OR equal to B A <= B # A is less than OR equal to B A <= B # A is less than OR equal to B

Boolean Expressions All Boolean expressions in Python, we say “evaluate” to either “True” or “False” All Boolean expressions in Python, we say “evaluate” to either “True” or “False”

Writing a Boolean Expression sticks_stones = 10 words = 7 if sticks_stones > words: # sticks_stones > words print( “Sticks and stones# 10 > 7 may break my bones, but# True, condition met words will never hurt me” )

Practice # given these variables # evaluate A = 99 A > B B = 7 B < C C = -5 B >= C D = 92 C <= D A == B + D D <= A + C C != B

Practice # given these variables # evaluate A = 99 A > B True B = 7 B < CFalse C = -5 B >= C True D = 92 C <= D True A == B + D True D <= A + C True C != BTrue

More Boolean Operators Don’t confuse “==“ with “=“: Don’t confuse “==“ with “=“: “=“ is used to assign variables “=“ is used to assign variables “==“ is used to test if two values are equivalent “==“ is used to test if two values are equivalent We use “!=“ to test if two values are different (“not equal to”) We use “!=“ to test if two values are different (“not equal to”) “ =“ test for more than one relationship at a time “ =“ test for more than one relationship at a time

Challenge: Guess the Magic Number Write a program that sets a magic number, anywhere from Write a program that sets a magic number, anywhere from Then, ask the user to guess a number. Then, ask the user to guess a number. If they guess correctly, then print out “You guessed correctly, the number was __!” If they guess correctly, then print out “You guessed correctly, the number was __!” If not, tell them, “Sorry, the magic number was __.” If not, tell them, “Sorry, the magic number was __.”

Challenge: Overtime Pay Write a program that asks the user for an hourly rate of pay and the number of hours they’ve worked Write a program that asks the user for an hourly rate of pay and the number of hours they’ve worked If the number of hours worked is over 40, then calculate for overtime pay at 1.5 the original rate If the number of hours worked is over 40, then calculate for overtime pay at 1.5 the original rate Then show the user how much money they made for the week Then show the user how much money they made for the week

Challenge: Overtime Pay HINT: you may want to set a variable for the overtime pay and set it equal to zero, before the selection statement HINT: you may want to set a variable for the overtime pay and set it equal to zero, before the selection statement This way, the overtime pay is only changed if the condition is met This way, the overtime pay is only changed if the condition is met You may want to do the same with the base You may want to do the same with the base