Download presentation
Presentation is loading. Please wait.
Published byFlorence Richards Modified over 9 years ago
2
These Guys? Wait, What? Really?
3
Branching is a fundamental part of programming It means taking an action based on decision The decision is dependent on a condition if hungry take a bite if thirsty take a drink
4
If statements must have a condition The statement, “It’s 100 degrees outside” ◦ Is either true or false; we say, “evaluates to true or false” ◦ Helps us determine what to do next
5
Comparison Operators help us evaluate conditions to true or false OperatorMeaningSample Condition Evaluates To ==Equal to5 == 5True !=Not equal to 8 != 5True >Greater than 3 > 10False <Less than5 < 8True >=Greater than or equal to 5 >= 10False <=Less than or equal to 5 <= 5True
6
By indenting a line, it becomes a block A block is one or more consecutive lines indented by the same amount Indenting sets lines off visually and logically If (authenticated is True) : print(“Access Granted”) print(“Welcome!”)
7
If Followed By A Condition Followed By A Colon Followed By A Block If the condition evaluates to ‘True’, the block is executed
8
Helps make a choice based on a condition Do one thing if the condition is true, else do something else if it is false password = input(“Please enter password”) if (password == ‘secret’) : print(“Access Granted”) else : print(“Access Denied”)
9
elif is short for ‘else if’; programmers are lazy and don’t want to type ‘else if’ when they can type ‘elif’ Allows the programmer to create a sequence of conditions to be evaluated
10
if (testScore >= 90) : grade = “A” elif (testScore >= 80) : grade = “B” elif (testScore >= 70) : grade = “C” elif (testScore >= 60) : grade = “D” else : grade = “F”
11
You can add more than one condition in the overall condition by adding ‘and’ and ‘or’ The overall condition is still evaluated to True or False if (hungry and thirsty) : takeABite() takeADrink() if (hungry or thirsty) stopAtMcDonalds()
12
Write a main function Ask the user for his/her age and store it in a variable ‘age’ Convert age to an integer using the int() function If your age is greater than 18, print, “You can vote” If the age is less than 18 print, “You cannot vote”
13
Write a main function that asks the user for his/her age and store it in a variable ‘age’ Ask the user if he or she is a citizen. (yes or no) and store the result in a variable ‘citizen’ If your age is > 18 and you are a citizen, print, “You can vote”. Otherwise, print, “You cannot vote”
14
Write a main function. Ask the user to enter the temperature; convert it to an integer If the temperature is > 80, turn the air conditioner on and make sure the heater is off If the temperature is between 65 and 79 turn the air conditioner off and make sure the heater is off If the temperature is below 65 turn the heater on and make sure the air conditioner is off
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.