1 / 21

Conditional Statements

Conditional Statements. Introduction to Computing Science and Programming I. Conditional Statements. So far we have only talked about writing statements that are executed one after another from top to bottom This simply isn’t sufficient to write very many useful programs. Simple Guessing Game.

roy
Download Presentation

Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 isn’t 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 isn’t “yes”, then write “Nuts.” write “That’s 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 isn’t “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 x==1: 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 > 5: • print “x is greater than 5” • if x > 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 = • x=5, assigns x the value 5 • x==5, 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 • <, >, <=, >= all act as you’d expect

  13. Boolean Operators • Logical Operators • and, & • A and B is true if both A and B are True x=5 (x > 1 and x < 4) is False • or, | • A or B is true if either A or B is True x=5 (x > 1 or x < 4) is True • not, ! • not A is true if A is False x=5 not x < 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 x=int(raw_input(“Enter an integer between 0 and 10: ”)) xIsInCorrectRange = (x >= 0 and x <= 10) if not xIsInCorrectRange: print “You didn’t enter an integer between 0 and 10” print “I’m giving you the number 5” x=5 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 >1000: print "It weighs " + str(weight/1000.0) + " kilograms." elif weight > 100: print "It weighs " + str(weight/100.0) + " hectograms." elif weight > 10: print "It weighs " + str(weight/10.0) + " dekagrams." elif weight >= 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 x==1: . . elif x==2: . . elif x==3: . . else: print “You didn’t enter a valid option.”

More Related