True/false: the if-then-else statement can be used to simplify a complex nested decision structure.

Presentation on theme: "Logical Operators Operators that can be used to create complex Boolean expressions and or not."— Presentation transcript:

1

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

3 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

4 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

5 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

6 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 = # 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’)

7 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

8 Modified Loan Qualifier Program (or)
# Notice the use of or # This program determines whether a bank customer qualifies for a loan. MIN_SALARY = # 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’)

9 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

10 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

11 Boolean Variable Examples
hungry = True door_open = False if sales >= : 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:’

12 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)

13 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)

14 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()

15 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()

16 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')

17 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')

18 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)

19 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)

20 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

Can a nested decision structure achieve the same logic as a case structure?

A nested decision structure can achieve the same logic as a case structure. Correct! The first line of the case structure starts with the word CASE followed by the test expression. The If-Then-Else statement can be used to simplify a complex nested decision structure.

Can a nested decision structure be used to test more than one condition?

The correct answer is true. Both nested decision and case structure used to test multiple conditions. The individual can achieve the same result by applying the same logic with the help of a nested decision structure or a case structure.

What is a nested decision?

The nesting of decision structures allows a program to sequentially determine the current state of a problem component under investigation. One might find an analogy to this process in games like "20 questions", where a person attempts to guess what another has in mind by asking a series of yes/no questions.

Which structure is a logical design that controls the order in which a set of statements executes control sequence module terminal none of these?

A control structure is a logical design that controls the order in which a set of statements execute.