Unit 3 Lesson 6 Conditionals
What is a conditional?
A conditional is a statement that affects the flow/outcome of a program by executing different statements based on the result of a true or false statement. That true or false statement is a boolean expression.
Conditionals appear in almost every programming language because of how important it is for changing the flow of the program.
Conditionals statements are used by the selection process for a program.
Question: What statement most commonly fills the role of a conditional in almost all programming languages?
if ("condition"):
("first statement")
else:
("second statement")
If/Else Statement can have more than one if condition.
If/Else Statement Example:
number = 3 # value that will affect true or false statement
if number == 5: # if part determines if the statement is true or false compared to another part of the program.
print("yes, 5 does equal 5")
else: #else part only executes if the if part is false
print("no, " + str(number) + " does not equal 5")
If/Else Statements can also just be if statements. This can be used more as an interruption rather then yes or no response.
If Statement Example:
progress = 0
while progress < 100:
print(str(progress) + "%")
if progress == 50:
print("Half way there")
progress = progress + 10
print("100%" + " Complete")
grade = int(input("Insert your grade percentage of a test you have took."))
if grade >= 60:
print("good job")
else:
print("That's a shame")
print("Did you study?")
study = input("Did you study for that test? Yes or No?")
study = study.lower()
if (study == "yes") & (grade >= 60):
print("Great Job!")
if (study == "no") (grade >= 60):
print("You're just built different")
if (study = "yes") & (grade < 60):
print("Ouch")
if (study == "no") & (grade < 60):
print("Study next time")
if (study != "yes") & (study != "no")
print("type YES or NO next time.")