# Conditional Statements Introduction to Computing Science and Programming I.

## Presentation on theme: "Conditional Statements Introduction to Computing Science and Programming I."— Presentation transcript:

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 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 This simply isn’t sufficient to write very many useful programs

Simple Guessing Game Here is an algorithm for a very 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.”

Simple Guessing Game Looking at this algorithm we see that there are statements that will be run under certain conditions, but not others. 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.”

Simple Guessing Game Depending on the value of answer we will do one of two different things. 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 If you want to write code like this that will execute if a certain condition is true, you use an if structure

If Structure if condition: statement1statement2statement3 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 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

Blocks of Code Python uses indentation to indicate 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 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”

Blocks of Code Blocks of Code can be within other blocks of code Blocks of Code can be within other blocks of code 1 if x > 5: 2 print “x is greater than 5” 3 if x > 10: 4 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 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

Guessing Game Here is how the guessing game is translated into Python 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."

The bool Data Type Before this point all of the expressions we have used evaluate to either a string, integer, or float. 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 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

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

Boolean Operators Comparison Operators Comparison Operators ==, is equal to ==, is equal to This is different from = This is different from = x=5, assigns x the value 5 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 x==5, a boolean expression that evaluates to True if x is equal to 5, False if is not !=, is not equal to !=, is not equal to Returns the opposite of == Returns the opposite of == x!=5 is true if x does not equal 5 x!=5 is true if x does not equal 5, = all act as you’d expect, = all act as you’d expect

Boolean Operators Logical Operators Logical Operators and, & and, & A and B is true if both A and B are True A and B is true if both A and B are Truex=5 (x > 1 and x 1 and x < 4) is False or, | or, | A or B is true if either A or B is True A or B is true if either A or B is Truex=5 (x > 1 or x 1 or x < 4) is True not, ! not, ! not A is true if A is False not A is true if A is Falsex=5 not x < 4 is True

Boolean Variables Since bool is a data type, you can store a bool value into a variable as with any other data type 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 = 0 and x <= 10) if not xIsInCorrectRange: print “You didn’t enter an integer between 0 and 10” print “You didn’t enter an integer between 0 and 10” print “I’m giving you the number 5” print “I’m giving you the number 5” x=5 x=5 print “Your number is “ + str(x) + “.”

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 If the condition is satisfied when execution reaches the if statement, then code block1 will be executed. Otherwise, code block2 will be executed

else Clause In our guessing game example we can simplify the code using an else clause instead of a second if statement 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 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. 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.”

elif Clauses elif is how Python shortens else-if 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 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

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. 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.

elif Clauses One more extension of the guessing game using an elif clause. 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’.“

elif Clauses Remember that only the first elif clause whose condition is satisfied will have its block of code executed. 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." print "It weighs " + str(weight/1000.0) + " kilograms." elif weight > 100: print "It weighs " + str(weight/100.0) + " hectograms." print "It weighs " + str(weight/100.0) + " hectograms." elif weight > 10: print "It weighs " + str(weight/10.0) + " dekagrams." print "It weighs " + str(weight/10.0) + " dekagrams." elif weight >= 0: print "It weighs " + str(weight) + " grams." print "It weighs " + str(weight) + " grams."else: print "You entered an invalid number." print "You entered an invalid number."

elif Clauses Easy way to set up a simple menu. 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.”