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?
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")