3.2 if Statements and Control Flow

Cards (79)

  • The path the program takes during execution based on a condition is called control flow.
  • Steps in how a conditional statement works
    1️⃣ Evaluate the boolean expression
    2️⃣ Execute the code block if true
    3️⃣ Follow the control flow
  • Comparison operators such as `<` are used to determine the truth or falsity of an expression in an `if` statement.
  • Which logical operator inverts the boolean value of a condition?
    NOT
  • Conditional statements determine the path the program takes based on the outcome of a boolean expression.

    True
  • If the condition in an `if` statement is false, the code block is skipped.

    True
  • The `OR` operator in `if (score >= 70 || attendance >= 90)` requires only one condition to be true for the entire expression to be true.
  • What output will the code snippet `if (score >= 60) { System.out.println("Pass"); } else { System.out.println("Fail"); }` produce if `score` is 45?
    Fail
  • The `else` clause is executed when the `if` condition is true
    False
  • What is the purpose of conditional statements in programming?
    Control program flow
  • What does a boolean expression evaluate to?
    True or false
  • What is the fundamental conditional statement in programming?
    if statement
  • Match the logical operator with its meaning:
    AND ↔️ Returns true if both conditions are true
    OR ↔️ Returns true if at least one condition is true
    NOT ↔️ Inverts the boolean value
  • What is the primary purpose of conditional statements in programming?
    Control program flow
  • In the provided example, what output will the program produce if the score is 75?
    Pass
  • What will the code `if (score >= 60) { System.out.println("Pass"); }` print if `score` is 55?
    Nothing
  • The `NOT` operator inverts the boolean value of a condition.

    True
  • What will the code `if (!isAbsent) { System.out.println("Student is present"); }` print if `isAbsent` is true?
    Nothing
  • The `else` clause provides an alternative code block to be executed when the `if` condition is false.
  • Match the term with its meaning:
    `else` clause ↔️ Alternative code block for false conditions
    `if-else` statement ↔️ Two paths based on a condition
  • Order the features of conditional statements, loops, and sequential statements based on control flow:
    1️⃣ Conditional Statements: Flexible
    2️⃣ Loops: Repetitive
    3️⃣ Sequential Statements: Linear
  • If the condition in an `if` statement is false, the code block inside it is skipped

    True
  • If the `AND` operator is used, both conditions must be true for the code block to execute

    True
  • The AND (`&&`) logical operator combines conditions in `if` statements.
  • The AND (`&&`) logical operator returns true only if both conditions are true.
  • The AND (`&&`) logical operator requires both conditions to be true to return true.

    True
  • The OR (`||`) logical operator returns true if at least one condition is true.
  • The basic syntax of a single `if` statement includes a condition.
  • The `else` clause extends the `if` statement by providing a code block for when the condition is false.
  • The `if-else` statement creates two possible paths based on a condition.
    True
  • What does the `elseif` statement allow programmers to do in a single `if-else` structure?
    Check multiple conditions
  • The `elseif` statement provides a more concise and readable approach compared to multiple if statements.
  • What grade is assigned to a score of 85 using the example code provided?
    B
  • If a student scores 85 and has 92% attendance, the output is "Passed the test" and "Excellent attendance".
  • The condition in a conditional statement must be a boolean expression that evaluates to true or false.
    True
  • A boolean expression is a key component of the condition in a conditional statement.
  • The condition in an if statement must be enclosed in parentheses.

    True
  • The `&&` operator returns true if at least one of the conditions is true.
    False
  • In a conditional statement, the condition is a boolean expression.
  • The basic syntax of an `if` statement includes the keyword `if` followed by a condition in parentheses.