0% found this document useful (0 votes)
15 views34 pages

3-2 Decision Operators

This document discusses decision structures and if statements in Python. It provides examples of using if statements to check conditions and execute code blocks based on those conditions being true or false. It also covers else statements, logical operators, nested if statements, and if/else ladders to handle multiple conditions. The key aspects covered are the syntax of if statements, logical expressions that if statements check, and how to structure programs to make different decisions using if, else, and nested if/else blocks.

Uploaded by

jdskk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views34 pages

3-2 Decision Operators

This document discusses decision structures and if statements in Python. It provides examples of using if statements to check conditions and execute code blocks based on those conditions being true or false. It also covers else statements, logical operators, nested if statements, and if/else ladders to handle multiple conditions. The key aspects covered are the syntax of if statements, logical expressions that if statements check, and how to structure programs to make different decisions using if, else, and nested if/else blocks.

Uploaded by

jdskk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Decision Structures

Section 2
Chapter 3
Decisions (If statement)
EX: test1.py
• The if statement
num = 1
– if condition:
if num>=1:
(tab) statement print('greater')
if num<1:
– condition: logical expression, true or false print('lower')
– logical expression
• 1: true, 0: false, other numbers: true
num = 1
>> 2>0
>> -1>0
– relational operators num >= 1 print('greater')
• < <= ==
• != > >=
• x = 3>2
• x = 3 == 3
• x = 0<0.5<1 num < 1 print(‘lower')
• x != 0, x != 2
Example
• balance = eval(input( 'Enter bank balance
(0~1000): ' ))
• If balance is lower than 500 then compute
the 500-balance & print.
• If balance is higher than 500 then compute
the 1000-balance & print.
• Use the if statement
if-else Statements
EX: test1.py
num = 1
if num>=1:
print('greater')
if num<1:
print('lower')

num = 1
if num>=1:
(else)
num >= 1 print('greater')
else:
print('lower')
print(‘lower') print('greater')
Example
• balance = eval(input( 'Enter bank balance
(0~1000): ' ));
• If balance is lower than 500 then compute
the 500-balance.
• If balance is higher or same than 500 then
compute the 1000-balance.
• Use the if, else statement
Basic form of if-else
• Basic form • elseif ladder
if conditionA: if condition1:
statementsA statementsA
If conditionB: elif condition2:
statementsB statementsB
… elif condition3:
statementsC

else:
num = 1 num = 1 statementsE
if num<1: if num<1:
print(‘low') print(‘low')
if 1<num<5: elif num<5:
print(‘middle') print(‘middle')
if num>5: else:
print(‘high') print(‘high')
Example
• balance = eval(input( 'Enter bank balance: (0~1000)' ));
• If balance is lower than 100 then compute the 100-balance.
• If balance is higher than 100 and lower than 300 then compute the 300-bal-
ance.
• If balance is higher than 300 and lower than 500 then compute the 500-bal-
ance.
• If balance is higher than 500 then compute the 1000-balance.
• Use if, elseif, else statment
Logical operators
lex1 lex2 not lex1 lex1 and lex2 lex1 or lex2
• Logical operators F F T F F
– and, or, not
F T T F T

T F F F T

T T F T T

if (b^2 - 4*a*c == 0) & (a ~= 0):


Example
x = -b / (2*a)

num = 1 num = 1
if num<1: if num<1:
print(‘low') print(‘low')
if 1<num<5: if num>1 and num<5:
print(‘middle') print(‘middle')
if num>5: if num>5:
print(‘high') print(‘high')
Elseif and if
Bank interest rate: <$5000  9%,
$5000≤ and <$10000  12%, ≥$10000  15%
• elseif • if
bal = 15000 * rand bal = 15000 * rand
if bal < 5000: if bal < 5000:
rate = 0.09 rate = 0.09
elif bal < 10000: if bal >= 5000 and bal < 10000:
rate = 0.12 rate = 0.12
else: if bal >= 10000:
rate = 0.15 rate = 0.15

newbal = bal + rate * bal print( newbal = bal + rate * bal


‘Rate & New balance is: ' ) print( 'Rate & New balance is: ' )
print(rate, newbal) print( rate, newbal )
Example
• balance = eval(input( 'Enter bank balance: (0~1000)' ))
• If balance is lower than 100 then compute the 100-balance.
• If balance is higher than 100 and lower than 300 then compute the
300-balance.
• If balance is higher than 300 and lower than 500 then compute the
500-balance.
• If balance is higher than 500 then compute the 1000-balance.
• Use if and logical operator
Elseif and if
Mother if Mother if Nested if
T T
con1 con2
nested if
F F State

num = 1 num = 1 num = 1


if num<1: if num<1: if num<1:
print(‘low') print(‘low') print(‘low')
if 1<num<5: if num>1 and num<5: if num>1:
print(‘middle') print(‘middle') if num<5:
if num>5: if num>5: print(‘middle')
print(‘high') print(‘high') if num>5:
print(‘high')
nested if
Bank interest rate: <$5000  9%,
$5000≤ and <$10000  12%, ≥$10000  15%

bal = 15000 * rand bal = 15000 * rand


if bal < 5000: if bal < 5000:
rate = 0.09 rate = 0.09
if bal >= 5000 and bal < 10000: if bal >= 5000:
rate = 0.12 if bal < 10000:
if bal >= 10000: rate = 0.12
rate = 0.15 # else statement 도 가능
end if bal >= 10000:
newbal = bal + rate * bal rate = 0.15
print( 'Rate & New balance is: ' ) end
print( rate, newbal ) newbal = bal + rate * bal
print( 'Rate & New balance is: ' )
print( rate, newbal )
Example
• balance = eval(input( 'Enter bank balance: (0~1000)' ))
• If balance is lower than 100 then compute the 100-balance.
• If balance is higher than 100 and lower than 300 then compute the
300-balance.
• If balance is higher than 300 and lower than 500 then compute the
500-balance.
• If balance is higher than 500 then compute the 1000-balance.
• Use if and nested if
Input Validation with if-elif-else State-
ments
• Example 9: Program uses the method is-
digit to guard against improper input.
Input Validation with if-elif-else State-
ments
• Example 9: Program uses the method is-
digit to guard against improper input.
if-else Statements
• Example 1: Program finds larger of two
numbers input by user.
if-else Statements

FIGURE 3.1 Flowchart for the if-else statement in Example 1.

© 2016 Pearson Education, Inc.,


Hoboken, NJ.  All rights reserved.
if-else Statements
• Example 2: if-else statement in program
has relational operators in its condition.
if Statements

• The else part of an if-else statement can


be omitted
• When the condition is false
– Execution continues with line after the if
statement block
if Statements
• Example 3: Program contains two if
statements
if Statements

FIGURE 3.2 Flowchart for Example 3.


Nested if-else Statements
• Indented blocks of if-else and if statements can
contain other if-else and if statements
– The if-else statements are said to be nested
• Consider the task of interpreting a beacon
– The color of the beacon light atop Boston’s
old John Hancock building forecasts the
weather
Nested if-else Statements
• Example 4: Program requests a color
(Blue or Red) and a mode (Steady or
Flashing) as input
– Then displays the weather forecast
Nested if-else Statements
• Example 4: Program requests a color
(Blue or Red) and a mode (Steady or
Flashing) as input
– Then displays the weather forecast
Nested if-else Statements
• Example 5: Program requests costs, rev-
enue for company
– Displays “Break even”, profit, or loss
Nested if-else Statements
• Example 5: Program requests costs, rev-
enue for company
– Displays “Break even”, profit, or loss
The elif Clause
• Allows for more than two possible alter-
natives with inclusion of elif clauses.
The elif Clause
• Example 6: Program reports if the two
numbers are equal.
The elif Clause
• Example 7: Program calculates a single
employee’s FICA tax withheld.
The elif Clause
• Example 7: Program calculates a single
employee’s FICA tax withheld.
The elif Clause
• Example 8: program assumes that the
user will graduate and determines if they
will graduate with honors.
The elif Clause

Figure 3.3 Program and Flowchart for Example 8.


True and False
• Example 10: Program illustrates truth
values of objects
HW
• 26(if-else), 30(if-else), 32(if-elseif),
34(nested if), 35(if-and-or), 36(if-and-or),
37(if-elif) 40(if-ifelse  if-if), 43(nested if)

You might also like