Conditional Statements - PowerPoint PPT Presentation

About This Presentation
Title:

Conditional Statements

Description:

For the guessing game we have two conditional statements that contain simple ... In our guessing game example we can simplify the code using an else clause ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 22
Provided by: chr15
Category:

less

Transcript and Presenter's Notes

Title: Conditional Statements


1
Conditional Statements
  • Introduction to Computing Science and Programming
    I

2
Conditional Statements
  • So far we have only talked about writing
    statements that are executed one after another
    from top to bottom
  • This simply isnt sufficient to write very many
    useful programs

3
Simple Guessing Game
  • Here is an algorithm for a very simple guessing
    game.
  • write Think of a number between 1 and 10.
  • set guess to 6.
  • write Is your number equal to guess?
  • read answer
  • if answer is yes, then
  • write I got it right!
  • if answer isnt yes, then
  • write Nuts.
  • write Thats the end of the game.

4
Simple Guessing Game
  • Looking at this algorithm we see that there are
    statements that will be run under certain
    conditions, but not others.
  • if answer is yes, then
  • write I got it right!
  • if answer isnt yes, then
  • write Nuts.

5
Simple Guessing Game
  • Depending on the value of answer we will do one
    of two different things.
  • If you want to write code like this that will
    execute if a certain condition is true, you use
    an if structure

6
If Structure
  • if condition
  • statement1
  • statement2
  • statement3
  • If the condition is satisfied when execution
    reaches the if statement, then all statements
    that are indented after it will be executed. If
    the condition is not satisfied then they will be
    skipped and execution will continue after the
    indented statements

7
Blocks of Code
  • Python uses indentation to indicate blocks of
    code.
  • For an if statement we indent every statement in
    the block of code that will be executed based on
    the result of the if
  • if x1
  • print first line of code block
  • print last line of code block
  • print first line outside of code block

8
Blocks of Code
  • Blocks of Code can be within other blocks of code
  • if x gt 5
  • print x is greater than 5
  • if x gt 10
  • print x is also greater than 10
  • Lines 2, 3, and 4 are the block of code for the
    first if statement. Line 4 is the block of code
    for the second if statement

9
Guessing Game
  • Here is how the guessing game is translated into
    Python
  • print "Think of a number between 1 and 10.
  • guess 6
  • answer raw_input("Is your number equal to \
    str(guess) "? ")
  • if answer "yes"
  • print "I got it right!"
  • if answer ! "yes"
  • print "Nuts."
  • print "That's the end of the game."

10
The bool Data Type
  • Before this point all of the expressions we have
    used evaluate to either a string, integer, or
    float.
  • For the if statement we have a condition that
    Python will evaluate to be either True or False.
    These conditions are boolean expressions for
    which we have the bool data type

11
The bool Data Type
  • For the guessing game we have two conditional
    statements that contain simple boolean
    expressions
  • answer yes
  • Evaluates to True if answer is equal to yes
  • answer ! yes
  • Evaluates to True if answer is not equal to yes

12
Boolean Operators
  • Comparison Operators
  • , is equal to
  • This is different from
  • x5, assigns x the value 5
  • x5, a boolean expression that evaluates to True
    if x is equal to 5, False if is not
  • !, is not equal to
  • Returns the opposite of
  • x!5 is true if x does not equal 5
  • lt, gt, lt, gt all act as youd expect

13
Boolean Operators
  • Logical Operators
  • and,
  • A and B is true if both A and B are True
  • x5
  • (x gt 1 and x lt 4) is False
  • or,
  • A or B is true if either A or B is True
  • x5
  • (x gt 1 or x lt 4) is True
  • not, !
  • not A is true if A is False
  • x5
  • not x lt 4 is True

14
Boolean Variables
  • Since bool is a data type, you can store a bool
    value into a variable as with any other data type
  • xint(raw_input(Enter an integer between 0 and
    10 ))
  • xIsInCorrectRange (x gt 0 and x lt 10)
  • if not xIsInCorrectRange
  • print You didnt enter an integer between 0
    and 10
  • print Im giving you the number 5
  • x5
  • print Your number is str(x) .

15
else Clause
  • if condition
  • code block1
  • else
  • code block2
  • If the condition is satisfied when execution
    reaches the if statement, then code block1 will
    be executed. Otherwise, code block2 will be
    executed

16
else Clause
  • In our guessing game example we can simplify the
    code using an else clause instead of a second if
    statement
  • We have this, but we know that if the first
    condition is false the second is true
  • if answer yes
  • print I got it right.
  • if answer ! yes
  • print Nuts.
  • We can use an else clause. If the condition in
    the if statement is false execution will move to
    the associated else clause.
  • if answer yes
  • print I got it right.
  • else
  • print Nuts.

17
elif Clauses
  • elif is how Python shortens else-if
  • elif clauses can be used if there are multiple
    related conditions you want to check for and
    execute different code depending on which one is
    true

18
elif Clauses
  • if condition1
  • code block1
  • elif condition2
  • code block2
  • .
  • .
  • else
  • code block
  • If condition1 is true, code block1 will be
    executed and the other clauses will be skipped,
    otherwise if condition2 is true, code block2 will
    be executed, and so on. If none of the
    conditions are true and there is an else clause,
    the code block for that clause will be executed.
    You can use as many elif clauses as needed.

19
elif Clauses
  • One more extension of the guessing game using an
    elif clause.
  • if answer "yes"
  • print "I got it right!"
  • elif answer "no"
  • print "Nuts."
  • else
  • print "You must answer yes or no.

20
elif Clauses
  • Remember that only the first elif clause whose
    condition is satisfied will have its block of
    code executed.
  • weight int(raw_input("How many grams does the
    object weight? "))
  • if weight gt1000
  • print "It weighs " str(weight/1000.0) "
    kilograms."
  • elif weight gt 100
  • print "It weighs " str(weight/100.0) "
    hectograms."
  • elif weight gt 10
  • print "It weighs " str(weight/10.0) "
    dekagrams."
  • elif weight gt 0
  • print "It weighs " str(weight) " grams."
  • else
  • print "You entered an invalid number."

21
elif Clauses
  • Easy way to set up a simple menu.
  • print 1. Create a new file
  • print 2. Open a file
  • print 3. Exit
  • x int(raw_input(Select an option ))
  • if x1
  • .
  • .
  • elif x2
  • .
  • .
  • elif x3
  • .
  • .
  • else
  • print You didnt enter a valid option.
Write a Comment
User Comments (0)
About PowerShow.com