Conditionals: Difference between revisions
Mr. MacKenty (talk | contribs) No edit summary |
Mr. MacKenty (talk | contribs) No edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[file:arrows.png|right|frame|Programming basics<ref>http://www.flaticon.com/</ref>]] | [[file:arrows.png|right|frame|Programming basics<ref>http://www.flaticon.com/</ref>]] | ||
Programs generally run from the | Programs generally run sequentially, starting from the first instruction and then moving to the next instruction, in a step-by-step process. A computer will read line one, execute line one, and then go to line two, for example. | ||
It is common to include conditional statements to decide if a program should "do something else" if a specific condition is true or false. | It is common to include conditional statements to decide if a program should "do something else" if a specific condition is true or false. | ||
Line 22: | Line 22: | ||
* [https://computersciencewiki.org/index.php/Operators#Comaprison_operators Please see this link for conditional operators] | * [https://computersciencewiki.org/index.php/Operators#Comaprison_operators Please see this link for conditional operators] | ||
== code sample == | |||
== Conditional code sample == | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
# this file helps us to understand multiple | |||
# this file helps us to understand conditionals in Python. | |||
# what do you think the output will be when you execute these instructions? | |||
a = 5 | |||
b = "bar" | |||
if a == 5: | |||
print("Yes, the variable a has been assigned to the value 5.") | |||
else: | |||
print("No, the variable a has not been assigned to the value 5.") | |||
# what do you think the output will be when you execute these instructions? | |||
a = 5 | |||
b = "bar" | |||
if b == "foo": | |||
print("yes, the variable b has been assigned the value foo") | |||
else: | |||
print("no, the variable b has been assigned to some other value than foo.") | |||
</syntaxhighlight> | |||
== Multiple conditionals code sample == | |||
<syntaxhighlight lang="python"> | |||
# this file helps us to understand multiple conditionals in Python. | |||
# what do you think the output will be when you execute these instructions? | |||
a = 5 | a = 5 | ||
Line 32: | Line 64: | ||
print("both conditions are true.") | print("both conditions are true.") | ||
else: | else: | ||
print("one of the conditions is | print("one or both of the conditions are false") | ||
# what do you think the output will be when you execute these instructions? | |||
a = 5 | |||
b = "bar" | |||
if (a == 5 or b == "bar"): | |||
print("both conditions are true.") | |||
else: | |||
print("one or both of the conditions are false") | |||
</syntaxhighlight> | |||
== Conditional code sample with lists == | |||
<syntaxhighlight lang="python"> | |||
# this file helps us to understand conditionals in Python. | |||
# what do you think the output will be when you execute these instructions? | |||
myList = ["foo","bar","baz"] | |||
if "foo" in myList: | |||
print("Foo is in the list") | |||
else: | |||
print("Foo is not in list") | |||
# what do you think the output will be when you execute these instructions? | |||
myList = ["foo","bar","baz"] | |||
if "bar" not in myList: | |||
print("bar is in the list") | |||
else: | |||
print("bar is not in list") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 14:32, 11 October 2021
Programs generally run sequentially, starting from the first instruction and then moving to the next instruction, in a step-by-step process. A computer will read line one, execute line one, and then go to line two, for example. It is common to include conditional statements to decide if a program should "do something else" if a specific condition is true or false.
A conditional statement evaluates an expression and executes instructions depending on the outcome of the evaluation. Conditionals depend on operators to evaluate if an expression is true or false. A condition and selection are not the same thing. A condition asks a question. A selection processes the answer.
With gratitude to and permission from Stephen Hughes (Coe College) and Philip East (University of Northern Iowa)[2], The list below is an example of types of conditional questions. You should start thinking about conditions in plain english before you start thinking about operators.
- Match
- Threshold
- Range
- One-of
- Not-match
- Not one of
- All of
- Some of
- Eligible
Conditional operators[edit]
Conditional code sample[edit]
# this file helps us to understand conditionals in Python.
# what do you think the output will be when you execute these instructions?
a = 5
b = "bar"
if a == 5:
print("Yes, the variable a has been assigned to the value 5.")
else:
print("No, the variable a has not been assigned to the value 5.")
# what do you think the output will be when you execute these instructions?
a = 5
b = "bar"
if b == "foo":
print("yes, the variable b has been assigned the value foo")
else:
print("no, the variable b has been assigned to some other value than foo.")
Multiple conditionals code sample[edit]
# this file helps us to understand multiple conditionals in Python.
# what do you think the output will be when you execute these instructions?
a = 5
b = "bar"
if (a == 5 and b == "bar"):
print("both conditions are true.")
else:
print("one or both of the conditions are false")
# what do you think the output will be when you execute these instructions?
a = 5
b = "bar"
if (a == 5 or b == "bar"):
print("both conditions are true.")
else:
print("one or both of the conditions are false")
Conditional code sample with lists[edit]
# this file helps us to understand conditionals in Python.
# what do you think the output will be when you execute these instructions?
myList = ["foo","bar","baz"]
if "foo" in myList:
print("Foo is in the list")
else:
print("Foo is not in list")
# what do you think the output will be when you execute these instructions?
myList = ["foo","bar","baz"]
if "bar" not in myList:
print("bar is in the list")
else:
print("bar is not in list")
Some videos[edit]