Conditional Statements: The `if` statement

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which programming construct is specifically designed to perform an action only when a certain condition is met?

  • Decision structure (correct)
  • Control structure
  • Sequence structure
  • Repetition structure

In a flowchart representing an if statement, what geometric shape is typically used to represent the condition that must be tested?

  • Diamond (correct)
  • Oval
  • Rectangle
  • Circle

What is the primary purpose of a ‘single alternative decision structure’?

  • To execute a block of code regardless of any condition.
  • To offer one alternative path of execution if the condition is true, otherwise exit. (correct)
  • To provide multiple paths of execution based on different conditions.
  • To loop through a set of instructions until a condition is met.

Which element is a required component of an if clause in Python syntax?

<p>A condition (B)</p> Signup and view all the answers

Based on the properties of relational operators, which of the following expressions will return True?

<p><code>a &lt;= b</code>, when <code>a</code> is 15 and <code>b</code> is 20 (D)</p> Signup and view all the answers

What is the key distinction between the == operator and the = operator in Python?

<p>The <code>==</code> operator checks for equality, while the <code>=</code> operator assigns a value. (B)</p> Signup and view all the answers

If x = 10 and y = 5, what will print(x != y) output?

<p>True (C)</p> Signup and view all the answers

In the construct of an if-else statement, what condition determines which block of code is executed?

<p>Whether the condition evaluates to true or false. (C)</p> Signup and view all the answers

What is an essential syntactical requirement for the else clause in relation to the if clause in Python?

<p>The <code>else</code> clause must be aligned with the starting <code>if</code> clause. (A)</p> Signup and view all the answers

What is a fundamental characteristic of string comparisons in Python?

<p>String comparisons are performed based on the ASCII values of the characters. (C)</p> Signup and view all the answers

How does Python evaluate strings when using greater than > or less than < operators?

<p>Character by character, according to the ASCII value of each character. (B)</p> Signup and view all the answers

If string a = 'apple' and string b = 'apricot', how will Python evaluate the expression a > b?

<p><code>False</code>, because 'apple' comes before 'apricot' lexicographically. (D)</p> Signup and view all the answers

What is the main purpose of logical operators in programming?

<p>To combine or modify Boolean expressions. (C)</p> Signup and view all the answers

What is the key characteristic of the and operator?

<p>It returns true only if both operands are true. (C)</p> Signup and view all the answers

Given x = True and y = False, what does print(x and y) output?

<p>False (B)</p> Signup and view all the answers

What is the primary behavior of the or operator?

<p>It returns true if at least one of the operands is true. (B)</p> Signup and view all the answers

Using the variables a = False and b = True, what would print(a or b) display?

<p>True (C)</p> Signup and view all the answers

In the context of Boolean expressions, what does ‘short-circuit evaluation’ refer to?

<p>Deciding the value of a compound Boolean expression after evaluating only one sub-expression. (C)</p> Signup and view all the answers

Which of the following scenarios demonstrates short-circuit evaluation with the and operator?

<p>If the left operand is false, the right operand is not evaluated. (A)</p> Signup and view all the answers

What is the primary function of the not operator in Python?

<p>To reverse the logical value of its operand. (C)</p> Signup and view all the answers

If x = True, what will print(not x) display?

<p>False (C)</p> Signup and view all the answers

What is the main purpose of a Boolean variable in programming?

<p>To reference one of two values: True or False. (B)</p> Signup and view all the answers

What is a common usage of Boolean variables, particularly in control flow?

<p>As flags to indicate whether a certain condition exists. (A)</p> Signup and view all the answers

How can you determine if a numeric value falls within a specific range in Python?

<p>By combining relational and logical operators. (D)</p> Signup and view all the answers

How would you express the condition 'x is between 10 and 20, inclusive' in Python?

<p><code>x &gt;= 10 and x &lt;= 20</code> (D)</p> Signup and view all the answers

Which operator in Python is used to determine if a given value is part of a sequence, like a string or list?

<p>in (A)</p> Signup and view all the answers

What will print('z' in 'alphabet') output?

<p>False (C)</p> Signup and view all the answers

What operator is used to verify that a certain value is not found within a sequence?

<p>not in (B)</p> Signup and view all the answers

If text = 'example' what is the result of print('amp' not in text)?

<p>True (D)</p> Signup and view all the answers

Signup and view all the answers

Flashcards

Control structure

A logical design that controls the order in which statements execute.

Sequence structure

Statements that execute in the order they appear.

Decision structure

Specific action(s) performed only if a condition exists.

Boolean expression

An expression tested by an if statement to determine if it is true or false.

Signup and view all the flashcards

Relational operator

Determines whether a specific relationship exists between two values.

Signup and view all the flashcards

= and <= operators

Tests two or more relationships; enough if ONE exists.

Signup and view all the flashcards

== operator

Tests if two operands are equal to one another.

Signup and view all the flashcards

!= operator

Tests if two operands are NOT equal.

Signup and view all the flashcards

Dual alternative decision structure

Two possible execution paths, determined by a condition.

Signup and view all the flashcards

Logical Operators

Operators (and, or, not) that create complex Boolean expressions.

Signup and view all the flashcards

AND/OR operators

Binary operators that connects two Boolean expressions

Signup and view all the flashcards

NOT operator

Unary operator that reverses the truth of its Boolean operand.

Signup and view all the flashcards

Short circuit evaluation

Deciding a compound Boolean expression's value after evaluating one sub-expression.

Signup and view all the flashcards

Boolean variable

References one of two values: True or False.

Signup and view all the flashcards

Flag

To specify the value of the variable

Signup and view all the flashcards

Check Numeric Range

Expression is true only when X is within range

Signup and view all the flashcards

Check Numeric Range

Expression is true only when X is out of range

Signup and view all the flashcards

The 'in' Operator

Determines if a value is in a sequence

Signup and view all the flashcards

The 'not in' Operator

If a value is not found in a sequence

Signup and view all the flashcards

Study Notes

The if Statement

  • Control Structure is a logical design that controls the order in which statements execute
  • Sequence structures are sets of statements that execute in the order they appear
  • Decision structures will perform a specific action only if a condition exists, also known as selection structure.
  • The flowchart diamond represents a true or false condition that must be tested
  • Actions are conditionally executed only when the condition is true
  • Single alternative decision structure provides only one alternative path of execution
  • The if clause includes the keyword if followed by the condition that can be true or false
  • When the if statement executes, if the condition is true, the block statements are executed, otherwise the block statements are skipped

Boolean Expressions and Relational Operators

  • Boolean expressions are tested by if to determine if it is true or false, e.g. a > b is true if a is greater than b, otherwise it is false
  • Relational Operators determine whether a specific relationship exists between two values, e.g. >, >=, <=, ==, !=
  • >= and <= operators test more than one relationship, and it is enough for one of the relationships to exist for the expression to be true
  • == determines whether the two operands are equal to one another and must not be confused with the assignment operator =
  • != operators determines whether the two operands are not equal
  • Relational operators can be used in a decision block, e.g. if balance == 0 or if payment != balance
  • A block can be inside another block, e.g. an if statement inside a function, statements in inner block must be indented with respect to the outer block

The if-else Statement

  • Dual alternative decision structure has two possible paths of execution
  • One path is taken if the condition is true, and the other if the condition is false
  • Syntax:
if condition:
   statements
else:
   other statements
  • the if and else clauses must be aligned
  • statements must be consistently indented

Comparing Strings

  • Strings can be compared using the == and != operators
  • String comparisons are case sensitive
  • Strings can be compared using >, <, >=, and <= by comparing character by character based on the ASCII values for each character
  • If a word is a substring of a longer word, the longer word is greater than the shorter word

Logical Operators

  • Logical operators create complex Boolean expressions
  • and and or operators are binary operators that connect two Boolean expressions into a compound Boolean expression
  • The not operator is a unary operator, that reverses the truth of its Boolean operand

The and Operator

  • Takes two Boolean expressions as operands
  • Creates a compound Boolean expression that is true only when both sub expressions are true
  • Truth Table
    • false and false = false
    • false and true = false
    • true and false = false
    • true and true = true
  • Can be use to simplify nested decision structures

The or Operator

  • Takes two Boolean expressions as operands
  • Creates compound Boolean expression that is true when either of the sub expressions is true
  • Truth Table
    • false or false = false
    • false or true = true
    • true or false = true
    • true or true = true
  • Can be use to simplify nested decision structures

Short Circuit Evaluation

  • Short Circuit evaluation is when value of a compound Boolean expression is decided after evaluating only one sub expression
  • The or and and operators perform this function
    • For the or operator, when the left operand is true, the compound expression is true. Otherwise, the right operand is evaluated
    • For the and operator, when the left operand is false, the compound expression is false. Otherwise, the right operand is evaluated

The not Operator

  • The not operator takes one Boolean expression and reverses its logical value
  • Truth Table
    • not false = true
    • not true = false
  • Use parentheses around an expression to clarify to what the not operator is being applied

Boolean Variables

  • Boolean variables reference one of two values, True or False and are represented by the bool data type
  • Booleans are commonly used as flags
    • A Flag is a variable that signals when some condition exists in a program
    • A Flag set to False means the condition does not exist
    • A Flag set to True means the condition does exists

Checking Numeric Ranges with Logical Operators

  • To determine whether a numeric value is within a specific range of values, use and, e.g. x >= 10 and x <= 20
  • To determine whether a numeric value is outside of a specific range of values, use or, e.g. x < 10 or x > 20

The in Operator

  • The in operator determines whether a given value is a constituent element of a sequence such as a string, e.g. "s" in "sebastian" is true

The not in Operator

  • The not in membership operator evaluates to true if it cannot find a variable in the specified sequence, and false otherwise, e.g. print("sss" not in "sebastian")

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser