Topic 4 - Decisions Flashcards

1
Q

What are if statements? Why are they needed on top of input, output and assignment statements? What type of data do they utilize?

A

If statements are statements which have a condition, and if that condition is true the computer reads the indented body, which has code telling the computer to output some value. If it is false, the body is bypassed and the computer moves on to the next line of code.

This is different then the other statements we have looked at because it allows us to skip lines in the code and not use every single one. It hence allows use to solve more complex problems.

Therefore, if statements permit or prevent another statement (in the body) from occurring. They allow us to test anything that can be determined to be true or false. therefore, they make use of Boolean data!

General form:
if condition:
Body
Don’t forget the colon after the if! It will automatically indent for you!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The condition portion of an if statement must be a __________ __________ , meaning it is _______ or _______. What can the condition be made up of (3)?

A

The condition portion of an if statement must be a Boolean result, meaning it is either true or false, there is no in between.
This condition can be made up of the value of a variable, the result of a function, or the result of a relational operator (which is the most common).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the relational operators? (6)

A

Relational operators compare two values and the result will be true or false (so this also uses Boolean data).
The 6 relational operators are:
1. Less than: < (if it is less than, it returns true. If larger then it returns false)
2. Greater than: >
3. Less than or equal to: <=
4. Greater than or equal to: >= (the thing on the left is greater than or equal to the thing on the right)
5. Equal: == (need two because one indicates an assignment operator)
6. NOT equal: !=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What values can be tested using relational operators (4)?
What types values can be tested?

A

Variables, literals (a sequence of digits), results from functions and expressions can all be tested. The types of values that are tested are integers, floats, booleans and strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can be combine multiple relational operators using Boolean logic? What are the 4 operators that we can use to do this? How can we figure out all possible different outputs that these operators can produce?

A

Using Boolean logic, we can combine relational operators using operators such as And, or, not or Xor.
These are used to form complex conditions, and by using TRUTH TABLES, we can list out all possible outputs that these operators can produce. Truth tables describe the behaviour of logical operators by listing off all the values that the operator acts on and indicating the output in the other column(s).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the “not” truth table look like? What are the two inputs you can chose to put into each spot in the truth table and what does each one represent?

A

The not operator just flips around the value of its operand, and so whatever input you put for the value will be the opposite for not of the value.

You can put in 1 or 0 into the truth table, as this is Boolean logic and therefore there are only two possible options. 0 represents false and 1 represents true (even though we said positive is 0 and negative is 1 before, it is flipped around here).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the “and” truth table look like?

A

The and truth table takes two operands (not just one like for the “not” operator) and produces on result. In order to be true, this operator requires BOTH inputs to be true, or both to be ones. Only then will the output also be true (or a one).

A B A and B
0 0 0
0 1 0
1 0 0
1 1 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you determine how many rows (horizontal) will be in the truth table? What is the formula?

A

The number of rows in the truth table is equal to 2 ^ (number of distinct variables). So for the not operator there is only one distinct variable, and 2^1 gives 2 distinct rows. For the and operator, there are two distinct variables coming in, and 2^2 =4, so there will be 4 distinct rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the or operator truth table look like? How many inputs does it take in?

A

The or operator truth table has 2^2 =4 rows, because it takes in two distinct operands and produces on result. This or is INCLUSIVE, meaning that it is essential AND OR. So either one can be true or BOTH can be true to make the or statement true. So the only condition where or is not true is where both the operands are zeroes (are false).

A B A and B
0 0 0
0 1 1
1 0 1
1 1 1 (inclusive or)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the Xor truth table look like, and what does this operator mean? How do the two operands values compare in a false case? What about in a true case?

A

This is the exclusive or, meaning that it does not allow both variables to be true. So it is only true if ONE OR THE OTEHR BUT NOT BOTH of the operands are true.

A B A and B
0 0 0
0 1 1
1 0 1
1 1 0

In a false case, the two operands have the same value, and in a true case the two operands have opposite values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Python does not include an xor operator, so what logical expressions can we use instead to achieve the same result? (3)

A

A xor B = (A or B) and (not (A and B))
Python evaluates brackets first, so it will find the truth value of A and B, and then flip that value using not. So if A and B is true then it has to be false.

Or you could do:
(A and not B) or (B and not A)

Or you could do:
A != B (saying they cannot be equal to each other, and this is then true if they have opposite values which is the same truth table as the exclusive or operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the not (A and B) truth table look like? What is this operation called? This operation is ___________ _________.

A

A B A and B Not (A and B)
0 0 0 1
0 1 0 1
1 0 0 1
1 1 1 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the not (A or B) truth table look like? What is this operator called? What other operand does this equal to?

A

A B A or B Not (A or B)
0 0 0 1
0 1 1 0
1 0 1 0
1 1 1 0

Not (A and B) is equal to the truth value of A and B. This is also functionally complete. This operator is called nor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a shortcut you can take for A or B and A and B operands? (Once you see a certain value coming in, what can you decide without even looking at the other value?) What is this shortcut called when a computer does it?

A

We can do short circuit evaluation here (allowing the computer to move much faster). For the and operator, as soon as we see one value is false, we know it must be false. This is the same as not (A or B). Then for A or B, if we see one is true, for the inclusive or we know the whole thing is true. This is the same as not (A and B).

So (A or B) = not (A and B)
And
(A and B) = not (A or B)
for truth functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Construct a truth table for A and (B or not C). First how many rows will there be? Then do half zeros and half ones for A, do 2 zeroes, 2 1s repeating for B, and then alternating for C. Then evaluate each part.

A

A B C Not C B or not C A and (B or not C)
0 0 0 1 1 0 (all false because
0 0 1 0 0 0 A is false).
0 1 0 1 1 0
0 1 1 0 1 0
1 0 0 1 1 1
1 0 1 0 0 0
1 1 0 1 1 1
1 1 1 0 1 1

To do this, we break it down into each piece based on order of operations. Python always starts inside the brackets and then moves outwards. So first find the truth value of each variable. Then apply the operators in the order of inside out. So first not C, then attach that to B using the or operator, and then use those truth values as the input for the overall operator which is AND.

So look for the main operator and always evaluate that last.
Then the last column including the whole expression includes the truth value result for each variable that was entered in with each specific value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the precedence of relational and logical and mathematical operators? List from highest precedence to lowest (6):
How do you know how to evaluate an equation with a bunch of operators?

A

Highest precedence:

  1. Mathematical operators ( +, /, - , *, FOLLOW BEDMAS WITHIN THIS)
  2. Relational operators (<, >, ==, etc.)
    EVALUATE FROM LEFT TO RIGHT WITHIN A LINE. NO ORDER FOR THESE RELATIONAL OPERATORS.
  3. Not (logical operator)
  4. And (logical operator)
  5. Or (Logical operator)
  6. Assignment (we always want the expression to the right to completely evaluate before we apply this value to the assignment operator and hence attach it to the variable).

Lowest precedence ^

When given an equation with a ton of operators, look for the operator of highest precedence and evaluate it using the values on either side of it. then look for the one with next highest precedence and evaluate the two terms on either side of it, and so on.

18
Q
  1. Evaluate w = 3 + 4 * 5 < 3 * 4 + 5 or 1 / 2 ! = 0:
  2. Then evaluate this if statement knowing this input:

a= false
b = false
c = true
x = 5

if a or b and c or 1 < x and x < 10:
print(x)

A
  1. What is the highest precedence operator? Well its mathematical operators, and then using BEDMAS it would be multiplication or division, whichever is further to the right. Here we start with 4 * 5 so it becomes:
    w = 3 + 20 < 3 * 4 + 5 or 1 / 2 ! = 0

Then we look for the next multiplication which is 3 * 4 so:
w = 3 + 20 < 12 + 5 or 1 / 2 ! = 0

Then we look for the next multiplication or division which is 1 / 2:
w = 3 + 20 < 12 + 5 or 0.5 ! = 0

Next, we do addition or subtraction from left to right so:
w = 23 < 17 or 0.5 ! = 0

Now we have relational and logical operators left. Relational comes first in the precedence so:
23 < 17 is false. And 0.5 != 0 is true.

Lastly, we combine this false and true using the logical operator or:
False or true (using the truth table) is TRUE!

So overall this statement is true, meaning the body of this statement would execute.

  1. Now for the 2nd question. We have to look for precedence. Relational operators come before logical operators, so we evaluate 1 < x and x < 10 first:
    In this case x = 5 so both these statements are true.

Then and comes before or in the precedence order, and we go from left to right.
So we evaluate b and c (which is false here), and then true and true (which is true here).

Now we have “a or false or true”.

So here a is false, so this becomes:
“False or false or true”. Since it is all or’s, we just evaluate from left to right:
False or false = false
Then false or true = true.

So overall this statement is true, and once again the body of the if statement would run!

19
Q

What are two equivalent if statements (using x ==):

A

If x: is equivalent to if x == true, because we are essentially saying with if x that if the condition evaluates as true, we will run the body, which is the same as saying if x is true. But this is redundant so we don’t need to show this.

If not x: Is equivalent to if x == false, because if x evaluates to false then you will run the body.

20
Q

When the body contains more then one statement what is this called? And how do we know which statements are included in the body?

A

This is called compound statements, and to know which statements are included in the body you look for the indent.
The body ends with the next line that is indented the same amount as the if statement.

21
Q

What is an if-then-else statement? Why is it used?
Can each if else pair independently run, even if you are checking the same variables?

A

What happens when we have a condition and we want to do something when it is true and something else when it is false, rather then just skipping over the whole thing if it is false?

Then we use an if-then-else statement! This occurs when we have two conditions. An if condition, and then an if not condition. However doing if and if not takes a lot of time, so instead we do if and then else. It essentially gives an alternative path to take if the if statement evaluates as false.

For these statements, only one will run. Because the if statement is either true or not, and the else is just the opposite value to the if statement.

Because only one or the other can run, the if and else are MUTUALLY EXCLUSIVE! Meaning that if one is true, the alternative must be false. So the “if then else” statements are used if the alternatives are mutually exclusive.

An if else statement is used for the mouse. If the mouse is in the document if acts as a curser, but if outside then it acts as a pointer.

Yes, each pair can independently run even if you are checking the same variables!

We use this when the conditions being checked are mutually exclusive, meaning that only one can be true while the other is false. We use elif when they are not mutually exclusive because that ensure that only one is run even though both could technically be true.

22
Q

What is an elif statement and why is it used? Why is it if, elif, else?

A

We start with if, and if it has a true value to its right then its over. If it doesn’t have a true value to its right then it moves onto the elif statement. Elif is just else if combined, so saying if that if is not true, then here is another case, and if it is true then carry out this body.

For elif statements, you can have a condition that would evaluate to true at the same time that an if statement above would evaluate to true. This is because elif ensures that only one statement runs, even if the conditions are not mutually exclusive (multiple could be correct at once). It just runs from top to bottom, and if the if is true, the elif (even if true as well) will be skipped.

ONE OF THEM AND ONLY ONE OF THEM WILL FIRE. AND THEY ARE CHECKED IN ORDER.

This essentially allows for a lot more checks of the data to see if it is true or not.

We can have as many elif clauses as we want, instead of putting if else, if else, if else, we can just put elif again and again. Elif must have a condition!

Else just follows if and will always follow if we get there!

23
Q

What statement do we use to test if a statement is true?

A

We use the if statement! If it is true, then the body will run.

24
Q

Why does else always fire if we get there?

A

Else always fires once we get to it because it essentially the opposite Boolean value to whatever comes before it. So if before it is all false, then it will be true. So if nothing else works in the code before, this will run and ensure that the code finishes.

25
So for a long code where you have a lot of conditions that need to be checked you would use the __ - ___ - ___ statement, where ______ is at the end and will evaluate if nothing else does.
So for a long code where you have a lot of conditions that need to be checked you would use the if - elif - else statement, where else is at the end and will evaluate if nothing else does. REMEMBER, IF AND ELIF BOTH REQUIRE A CONDITION TO RUN, BUT ELSE DOES NOT. IT JUST RUNS IF YOU GET TO IT.
26
What is testing? What can’t testing do? How do we know how many times to run the program to do this testing?
Testing is the process of executing a program in an attempt to locate bugs. Testing cannot prove that a program is bug free, it can only locate bugs if you put in the right input. We can determine how many times to run a programs based on its complexity and how bad it is if it doesn’t work.
27
What are two types of general testing?
Black-box testing: This is where you test the program without looking at the source code, so they are generally functional/behavioural. This essentially is testing if the program will behave the way we expect it to for certain inputs. This is black box because it is opaque (you cant actually see the code, you are just testing behaviourally). White-box testing: This is where you look at the source code and design test cases based on the structure. So this is called white box testing which is clear because we can see into the code.
28
What are the three types of white box test coverage? and how does each one work? Which covers the most ground to give the most certainty?
The three types of test coverage are: 1. Condition coverage: This is where you execute every decision point, but not every case of every decision point. this just ensures that the conditions are working, but will skip over a lot of the program if the if statements evaluate to false. 2. Statement coverage: This is where every statement in the program is executed, or in other words every line is run at least once. So we would use two test cases to make a true and false outcome for each condition. 3. Path coverage: Then path coverage ensures that every single path is covered through the program, not just every statement. This would be 4 test cases for the same example. This one covers the most ground and hence gives the most certainty for testing, however it is still not complete. This is because you can still enter a non-numerical number which could crash the program.
29
What are the dangers of floating point numbers? (Using the penny example) What can we test for instead of equalities when looking at floating point numbers?
Floating point numbers are only an approximation of realm numbers because there are infinite real numbers to represent. Therefore, they can cause problems when testing for equalities. For example, adding 0.01 to a number, only so many significant digits can be stored and so eventually it will be rounded. Therefore we can get incorrect answers due to incorrect rounding when looking at the difference between two amounts of cents. So instead we want to test for inequalities instead, because this accounts for the approximation that occurs with floating point numbers.