The if
statement
- The most common form of the
if
statement has syntax
if ( logical-expression ) :action1
else :
action2
where
logical-expression
is a test expression that evalutes to eitherTrue
orFalse
. Bothaction
1 andaction
2 must be non-empty statement lists. The actions are indented one-level further than the start of theif
statement. Theelse
is indented at the same level as theif
. Bothif
andelse
are keywords
- The semantics of the
if
statement is thatlogical-expression
is first evaluated. If the expression isTrue
,action
1 is executed; otherwise,action
2 is executed. No matter what only one of the two actions are executed.
- Consider the following code segment from if_else.py.
reply = input( 'Enter one or two words: ' )list = reply.split()
n = len( list )
if ( n == 1 ) :
# list size is 1
print( 'one word' )
else :
# otherwise, list size is 2
print( 'two words' )
By getting the input and turning it into a list, we can easily determine the number of words entered (i.e., the length of the list). By determining whether the list size is one. We can produce the appropriate response.
- Suppose our only concern was whether the user could follow instructions. The following code segment from if.py does so without using the optional
else
part of theif
statement.
reply = input( 'Enter one or two words: ' )list = reply.split()
n = len( list )
# assume they followed directions
response = 'You followed directions'
if ( ( n != 0 ) and ( n != 1 ) ) : # what does it take to be wrong? n is neither 0 nor 1
# they did not, so response needs updating
response = 'You did not follow directions'
print( response)
To correctly determine whether the input is too short or too long, the
if
test expression has multiple terms that areanded
-ed together. My advice always parenthesize each term in a logical expression. Although the problem is artificial and can be done other ways. It shows what was intended.
- Some programmers hate not having an
else
for everyif
. They want one there to explicitly show they considered the possibility. Because Python requires that a statement list be non-empty, some statement must be there. Python provides theapass
statement for such a purpose (pass
is a keyword). When Python encounters apass
statement, it just continues on to the next statement. Some programmers will use apass
statement as a temporary action as they develop code. Thepass
statement is a placeholder for code to be written.
- The below code segment from if_else_pass.py uses a
pass
statement statement in support of emptyelse
for our previous problem.
reply = input( 'Enter one or two words: ' )list = reply.split()
n = len( list )
# assume they followed directions
response = 'You followed directions'
if ( ( n != 0 ) and ( n != 1 ) ) : # what does it take to be wrong? n is neither 0 nor 1
# they did not, so response needs updating
response = 'You did not follow directions'
else :
# no action necessary
pass
print( response)
- Let's revisit the word entry problem again. Now are concern is to alert people who did not follow directions on what they did wrong and to acknowledge people who did follow directions.
- Python recognizes that often our decision process is not necessarily choosing one thing or another. Sometimes we need to test which of several conditions is in effect, and then take the appropriate action. We can use
elif
's to test for the various possibilites (when you read theelif
think else if ).
- Consider the following code segment from if_elif_else.py. The code segment uses the optional
elif
part of anif
statement.
reply = input( 'Enter one or two words: ' )list = reply.split()
n = len( list )
# what are the possibilities for the user response
# enters no words
# enters one word
# enters two words
# enters more than two words (i.e., it is not of the prior cases)
response = '' # none so far
if ( n == 0 ) :
# they did not follow directions -- no words were supplied
response = 'You did not enter any words'
elif ( n == 1 ) :
# they did follow directions by entering one word
response = 'Thanks for following directions by entering one word'
elif ( n == 2 ) :
# they did follow directions by entering two words
response = 'Thanks for following directions by entering two words'
else :
# they did not follow directions -- too many words supplied
response = 'You entered too many words'
print( response)
The above
if
statement recognizes there are four possibilities of concern when users react to the prompt: they did not enter any words, they entered one word, they entered two words, or they did something else (i.e., entered more than two words). For each of these possibilities, theif
statement takes the possibility into account and correctly responds.
© 2019 Jim Cohoon | Resources from previous semesters are available. |