All Flashcards
Why are Boolean values important in programming?
They are the backbone of conditional statements and loops, controlling program flow.
Explain the precedence of logical operators.
'NOT' is evaluated before 'AND', which is evaluated before 'OR'.
How do relational operators create Boolean values?
By comparing two values and returning 'true' or 'false' based on their relationship.
What is the purpose of a truth table?
To visualize how logical operators work for all possible combinations of inputs.
When is the 'AND' operator useful?
When you need to ensure that multiple conditions are met before executing a block of code.
When is the 'OR' operator useful?
When you need to execute a block of code if at least one of several conditions is met.
Explain how 'NOT' can simplify complex conditions.
It can be used to reverse a condition, making code more readable or efficient in certain cases.
Describe the role of Boolean logic in conditional statements.
Boolean expressions determine which branch of a conditional statement is executed.
How does Boolean logic relate to loops?
Boolean conditions control whether a loop continues to iterate or terminates.
What is the significance of understanding Boolean logic?
It is foundational for many other computer science concepts and is essential for problem-solving.
What does the following code output?
python
x = 10
y = 5
print(x == y)
False
What does the following code output?
python
x = 7
print(not x > 5)
False
What does the following code output?
python
x = 3
y = 8
print((x < 5) and (y > 10))
False
What does the following code output?
python
x = 12
y = 6
print((x > 10) or (y < 5))
True
What does the following code output?
python
x = 5
print(not (x != 5))
True
Identify the error in the following code:
python
x = 10
if x = 5:
print("x is 5")
Using '=' (assignment) instead of '==' (equality) in the if statement.
What does the following code output?
python
x = True
y = False
print(x and (not y))
True
What does the following code output?
python
x = 4
y = 4
print((x >= 4) and (y <= 4))
True
What does the following code output?
python
x = 2
y = 6
print(not (x > 5 or y < 3))
True
What does the following code output?
python
x = 9
y = 1
print((x != 10) and (y == 1))
True
What is a Boolean variable?
A variable that can be either 'true' or 'false'.
What is a relational operator?
A symbol that compares two values and returns a Boolean value.
What is a logical operator?
A symbol that combines or modifies Boolean expressions.
Define the 'NOT' operator.
Flips the Boolean value; 'true' becomes 'false' and vice versa.
Define the 'AND' operator.
Returns 'true' only if both conditions are 'true'.
Define the 'OR' operator.
Returns 'true' if at least one condition is 'true'.
What does the '==' operator do?
Checks if two values are equal.
What does the '!=' operator do?
Checks if two values are not equal.
What does the '>=' operator do?
Checks if the left value is greater than or equal to the right value.
What does the '<=' operator do?
Checks if the left value is less than or equal to the right value.