For each of the examples below, write the condition that would cause the displayed output.
Sample Program | Condition |
---|---|
Stop when x is odd. Programx = 28 while(____________________________): print(x) x = x // 2 print(x) Output28 14 7 |
|
Stop when x is greater than 21 Programx = 0 while(____________________________): print(x) x = 2 x + 1 print(x) Output0 1 3 7 15 31 |
|
Stops when x is greater than y Programx = 1 y = 200 while(____________________________): print(x, y) x = x * 2 y = y // 2 print(x, y) Output1 200 2 100 4 50 8 25 16 12 |
|
Stops when the user inputs a \(-1\). Programx = 0 while(____________________________): x = int(input()) print(x) print(x) Output5 2 6 3 2 1 -1 |
Demorgan's law is probably the most useful law of logic we use in computer science. It allows us to successfully negate our conditions. Demorgan's law states:
$$ \neg (p \wedge q) \equiv (\neg p \vee \neg q)\\ \neg (p \vee q) \equiv (\neg p \wedge \neg q) $$Use Demorgan's law to negate each of the following conditions. Draw a truth table to verify your negation.
x != 0 and x > y
x > y and y > z
not victory and score > 0
score > 0 or score < 0