Logical Operators Operators that can be used to create complex Boolean expressions and or not.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Making Decisions and Working With Strings
CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Chapter 4 Making Decisions
Selection in C.
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
Chapter 3 Making Decisions
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
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.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2.
CS0007: Introduction to Computer Programming
More on the Selection Structure
Basics programming concepts-2
Chapter 3: Decisions and Loops
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Python: Control Structures
Lecture 3- Decision Structures
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Introduction To Robot Sensors
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Javascript Conditionals.
JavaScript: Control Statements.
if-else-if Statements
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
2-1 Making Decisions Sample assignment statements
Lesson 04 Control Structures I : Decision Making
Chapter 3:Decision Structures
Making Logical Decisions (IF-THEN-ELSE)
Chapter 4: Decision Structures and Boolean Logic
Selection Control Structure
Suppressing print Newline
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
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
Conditional Logic Presentation Name Course Name
The System.exit() Method
Lecture 6: Conditionals AP Computer Science Principles
Decision I (if Statement)
Expressions.
Introduction to Turtle Graphics
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Life is Full of Alternatives
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
Chapter 4: Decision Structures and Boolean Logic
Controlling Program Flow
Presentation transcript:

Logical Operators Operators that can be used to create complex Boolean expressions and or not

The and Operator Takes two Boolean expressions as operands Creates compound Boolean expression that is true only when both sub expressions are true Can be used to simplify nested decision structures Truth table for the and operator Value of the Expression Expression false false and false false and true true and false true true and true

The or Operator Takes two Boolean expressions as operands Creates compound Boolean expression that is true when either of the sub expressions is true Can be used to simplify nested decision structures Truth table for the or operator Value of the Expression Expression false false and false true false and true true and false true and true

The not Operator Takes one Boolean expressions as operand and reverses its logical value Sometimes it may be necessary to place parentheses around an expression to clarify what you are applying the not operator to Truth table for the not operator Value of the Expression Expression false true

Modified Loan Qualifier Program (and) # Notice the use of and instead of multiple ifs. # This program determines whether a bank customer qualifies for a loan. MIN_SALARY = 30000.0 # The minimum annual salary MIN_YEARS = 2 # The minimum years on the job salary = float(input('Enter your annual salary: ')) years_on_job = int(input('Enter the number of ' + 'years employed: ')) # Determine whether the customer qualifies. if salary >= MIN_SALARY and years_on_job >= MIN_YEARS: print('You qualify for the loan.') else: print('You do not qualify for the loan’)

Modified Loan Qualifier Program (or) Qualifications for the loan has changed Someone qualifies for a loan if they earn at least $30,000 or they have worked at their job for at least 2 years

Modified Loan Qualifier Program (or) # Notice the use of or # This program determines whether a bank customer qualifies for a loan. MIN_SALARY = 30000.0 # The minimum annual salary MIN_YEARS = 2 # The minimum years on the job salary = float(input('Enter your annual salary: ')) years_on_job = int(input('Enter the number of ' + 'years employed: ')) # Determine whether the customer qualifies. if salary >= MIN_SALARY or years_on_job >= MIN_YEARS: print('You qualify for the loan.') else: print('You do not qualify for the loan’)

Checking Numeric Ranges with Logical Operators To determine whether a numeric value is within a specific range of values, use and Example: x >= 10 and x <= 20 To determine whether a numeric value is outside of a specific range of values, use or Example: x < 10 or x > 20

Boolean Variables Can be assigned to only one of two values, True or False Represented by bool data type Commonly used as flags Signals when some condition exists in a program Flag set to False  condition does not exist Flag set to True  condition exists

Boolean Variable Examples hungry = True door_open = False if sales >= 50000.0: sales_quota_met =True else: sales quota_met = False if sales_quota_met: print(‘you have met your sales quota!’) salary = base_salary + BONUS Could have also used ‘if sales_quota = True:’

Determining the State of the Turtle The turtle.xcor() and turtle.ycor() functions return the turtle's X and Y coordinates Examples of calling these functions in an if statement: if turtle.ycor() < 0: turtle.goto(0, 0) if turtle.xcor() > 100 and turtle.xcor() < 200: turtle.goto(0, 0)

Determining Turtle’s Heading The turtle.heading() function returns the turtle's heading. (By default, the heading is returned in degrees.) Example of calling the function in an if statement: if turtle.heading() >= 90 and turtle.heading() <= 270: turtle.setheading(180)

Determining If Pen is Down The turtle.isdown() function returns True if the pen is down, or False otherwise. Example of calling the function in an if statement: Notice in example that there is no == since value returned is Boolean if turtle.isdown(): turtle.penup() if not(turtle.isdown()): turtle.pendown()

Determining If Turtle Is Visible The turtle.isvisible() function returns True if the turtle is visible, or False otherwise. Example of calling the function in an if statement: if turtle.isvisible(): turtle.hideturtle()

Determining Pen and Fill Color When you call turtle.pencolor() without passing an argument, the function returns the pen's current color as a string. Example of calling the function in an if statement: When you call turtle.fillcolor() without passing an argument, the function returns the current fill color as a string. Example of calling the function in an if statement: if turtle.pencolor() == 'red': turtle.pencolor('blue') if turtle.fillcolor() == 'blue': turtle.fillcolor('white')

Determining the State of the Turtle When you call turtle.bgcolor() without passing an argument, the function returns the current background color as a string. Example of calling the function in an if statement: if turtle.bgcolor() == 'white': turtle.bgcolor('gray')

Determining Pen Size When you call turtle.pensize() without passing an argument, the function returns the pen's current size as a string. Example of calling the function in an if statement: if turtle.pensize() < 3: turtle.pensize(3)

Determining Speed of Turtle When you call turtle.speed() without passing an argument, the function returns the current animation speed. Example of calling the function in an if statement: if turtle.speed() > 0: turtle.speed(0)

Hit The Target Example Type in an angle to point the turtle to hit the square in the upper right Type in a force from 1 to 10 to reach the square Target is hit when inside the square