Introduction to Decision Structures and Boolean Variables

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
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
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Decision Structures, String Comparison, Nested Structures
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
 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.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Chapter 3 Selection Statements
CS0007: Introduction to Computer Programming
Line Continuation, Output Formatting, and Decision Structures
Chapter 6 Conditionals.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Python: Control Structures
Lecture 3- Decision 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,
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Microsoft Visual Basic 2005 BASICS
Introduction To Robot Decision Making
JavaScript conditional
Decision Structures, String Comparison, Nested Structures
Topics The if Statement The if-else Statement Comparing Strings
Conditionals & Boolean Expressions
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Bools and simple if statements
Decision Structures, String Comparison, Nested Structures
Decision Structures, String Comparison, Nested Structures
Loops CIS 40 – Introduction to Programming in Python
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
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
Understanding Conditions
JavaScript conditional
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
Using Decision Structures
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Introduction to C Programming
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Conditionals.
Chapter 6 Conditionals.
Presentation transcript:

Introduction to Decision Structures and Boolean Variables

Warm-Up Write a program that asks the user for their hourly rate of pay. Then, ask the user how many hours they worked this week and calculate and print their wages for the week. Format accordingly

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 We would now like for programs to deviate from the linear structure to adapt according to conditions being met, or not met

The Problem If you remember, when we were calculating the car payments, we left an option at the bottom of the code for if the price they were given was too high. The problem was, it wasn’t really an option … it was more like a forced secondary deal So, how do we get Python to make decisions …?

The Selection Statement Allows program to “ask a question” and respond accordingly 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)

The Selection Statement 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 Otherwise, we continue with the program as-is

The Selection Statement In Python, we use the keyword “if” to start a 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)

Writing a condition Python doesn’t read the way you would, so you can’t just write your conditions as questions like, “Is it cold outside?” The key is to ask a question and then compare the returned value (the answer to your question) to another value This comparison is our condition Each of these conditions must hold either “True” or “False”

Boolean Expressions

Example answer = input(“Is it cold outside?”) if answer == “yes” : # this will only execute IF and only IF the condition holds TRUE print(“Go put on a jacket!”) # then the program will return to it’s original line of code print(“Go outside.”)

Boolean Expressions Writing a condition

Boolean Expressions Named after George Boole, a 19th century English philosopher and mathematician 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

Boolean Expressions 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

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

Writing a Boolean Expression charizard = 10 pikachu = 7 if charizard > pikachu: # charizard > pikachu print( “It seems that your # 10 > 7 Pikachu is no match for the # True, condition met almighty Charizard” )

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 < 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 < C False 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 < C False C = -5 B >= C True 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 < C False C = -5 B >= C True D = 92 C <= D True A == B + D D >= A - C C != B

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

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

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

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

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