What does a conditional statement control?
A conditional statement controls which instructions execute by evaluating a condition as true or false and selecting an appropriate branch.
Study 04. Conditional Statements with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What does a conditional statement control?
A conditional statement controls which instructions execute by evaluating a condition as true or false and selecting an appropriate branch.
What syntax defines an `if` block in Python?
Python uses a colon after the condition and indentation to define the statements controlled by an `if` statement.
How does an `if-else` statement choose a branch?
An `if-else` statement chooses between two alternatives: the `if` branch runs when its condition is true; otherwise, the `else` branch runs.
What happens in an `elif` chain?
Python tests `elif` conditions from top to bottom and executes only the first branch whose condition is true.
How should chained conditions be ordered?
Put more specific or higher-priority conditions before broader ones; otherwise, the broad branch may match first and make later branches unreachable.
What is the difference between `==` and `=`?
Use `==` to compare values. A single `=` assigns a value to a variable rather than testing equality.
When is an `and` expression true?
`and` is true only when both of its conditions are true.
What do `or` and `not` do?
`or` is true when at least one condition is true, while `not` reverses a Boolean result.
Which Python values are generally false in conditions?
Values such as `0`, `None`, and empty collections are generally treated as false in Python conditions.
When is a nested condition evaluated?
A nested condition is an `if` statement inside another conditional block; the inner condition is evaluated only after the outer condition succeeds.
What is the purpose of validation?
Validation checks input form and allowed range before use, reports failures, and continues only when the input is valid.
What is a guard clause?
A guard clause handles an invalid or exceptional case immediately, often with `return`, so the main operation needs less nesting.