All Flashcards
Why are if statements important?
They allow programs to make decisions and respond dynamically to different situations.
Explain the purpose of an else statement.
To provide an alternative code path when the if condition is false, ensuring that some code always executes.
Describe the flow of execution in an if-else statement.
The condition in the if statement is evaluated. If true, the if block executes; otherwise, the else block executes. Only one block runs.
What is the significance of Boolean expressions in conditional statements?
Boolean expressions determine which code block is executed based on whether they evaluate to true or false.
Explain the concept of selection in programming.
Selection is a control flow mechanism that allows a program to choose between different paths of execution based on a condition.
How does indentation affect the structure of if and else blocks in Python?
Indentation defines the scope of the if and else blocks, indicating which statements belong to each block.
Why is it important to understand Boolean expressions?
They are the foundation of decision-making in programming, controlling the flow of execution based on conditions.
What is the role of conditional statements in creating dynamic and responsive programs?
Conditional statements allow programs to adapt to different inputs and situations, making them more flexible and user-friendly.
Explain how if and else statements contribute to the overall control flow of a program.
They provide a way to direct the execution of code based on conditions, allowing the program to follow different paths depending on the input or state.
What is the difference between an if statement and an if-else statement?
if executes code only when a condition is true. if-else provides an alternative path when the condition is false.
What is a conditional statement?
A statement that executes different code blocks based on a condition.
What is a Boolean expression?
An expression that evaluates to either true or false.
What is an if statement?
A conditional statement that executes a block of code if a condition is true.
What is an else statement?
A conditional statement that executes a block of code if the if condition is false.
Define 'selection' in programming.
Choosing which code block to execute based on a condition.
What is the role of indentation in Python if statements?
Indentation defines the code block that belongs to the if or else statement.
What is control flow?
The order in which statements are executed in a program.
Define relational operator.
Symbols used to compare values in conditional statements (e.g., ==, !=, >, <, >=, <=).
What is the purpose of a conditional statement?
To allow a program to make decisions and execute different code paths based on certain conditions.
What is meant by 'dynamic' and 'responsive' programs?
Programs that can adapt their behavior based on input or changing conditions.
What does the following code output?
python
x = 5
if x > 10:
print("Greater than 10")
else:
print("Less than or equal to 10")
Less than or equal to 10
What does the following code output?
python
age = 18
if age >= 18:
print("Eligible to vote")
Eligible to vote
What does the following code output?
python
score = 55
if score >= 60:
print("Pass")
else:
print("Fail")
Fail
What does the following code output?
python
is_raining = True
if is_raining:
print("Take an umbrella")
Take an umbrella
What does the following code output?
python
number = 0
if number > 0:
print("Positive")
else:
print("Not positive")
Not positive
Identify the error in the following code:
python
x = 5
if x > 10
print("Greater than 10")
Missing colon after the if condition.
Identify the error in the following code:
python
age = 15
if age >= 18:
print("Eligible to vote")
Missing indentation for the print statement inside the if block.
What does the following code output?
python
x = 10
y = 5
if x > y:
print("x is greater than y")
else:
print("x is not greater than y")
x is greater than y
What does the following code output?
python
fruit = "apple"
if fruit == "banana":
print("It's a banana")
else:
print("It's not a banana")
It's not a banana
What does the following code output?
python
temperature = 25
if temperature > 30:
print("It's hot")
else:
print("It's not hot")
It's not hot