Control Structures (College Board AP® Computer Science Principles): Flashcards

1/24

0Still learning

Know0

  • Define conditional execution.

Cards in this collection (24)

  • Define conditional execution.

    Conditional execution is a form of selection where a section of code runs only if a specified condition is true.

  • What happens when the condition of an IF statement is false?

    The block is skipped and control passes to the next statement after the IF block.

  • Why do programs need selection?

    Programs need selection so they can respond differently to different situations; without it, a program follows the same path regardless of input.

  • An IF statement evaluates a _______ condition and executes its block only if that condition is true.

    An IF statement evaluates a Boolean condition and executes its block only if that condition is true.

  • What is displayed when this code segment runs?

    temp ← 20
    
    IF(temp > 25)
    
    {
    
       DISPLAY("Hot")
    
    }

    Nothing is displayed, because 20 > 25 is false, so the IF block is skipped.

  • How many branches of an IF-ELSE statement execute?

    Exactly one branch of an IF-ELSE statement executes.

  • When should IF-ELSE be used instead of IF?

    IF-ELSE is used when a different action is needed for both the true and false cases.

  • Define nested conditional.

    A nested conditional is an IF or IF-ELSE statement placed inside another IF or IF-ELSE block.

  • True or False?

    In a nested conditional, the inner condition is always evaluated.

    False.

    The inner condition is only evaluated if execution reaches the branch that contains it.

  • What is displayed when this code segment runs?

    score ← 49
    
    IF(score ≥ 50)
    
    {
    
       DISPLAY("Pass")
    
    }
    
    ELSE
    
    {
    
       DISPLAY("Fail")
    
    }

    The code segment displays "Fail", because 49 ≥ 50 is false, so the ELSE branch executes.

  • What is displayed when this code segment runs?

    n ← 7
    
    IF(n MOD 2 = 0)
    
    {
    
       DISPLAY("Even")
    
    }
    
    ELSE
    
    {
    
       IF(n > 5)
    
       {
    
          DISPLAY("Big odd")
    
       }
    
       ELSE
    
       {
    
          DISPLAY("Small odd")
    
       }
    
    }

    The code segment displays "Big odd": 7 MOD 2 is 1, so the outer ELSE runs, and 7 > 5 is true.

  • Define iteration.

    Iteration is a repeating portion of an algorithm.

  • Name the two iteration statements listed under Iteration on the AP Exam Reference Sheet.

    The two iteration statements are REPEAT n TIMES and REPEAT UNTIL.

  • When should a REPEAT n TIMES loop be used?

    REPEAT n TIMES is used when the number of repetitions is known in advance.

  • When should a REPEAT UNTIL loop be used?

    REPEAT UNTIL is used when the number of repetitions depends on a changing value or user input.

  • How many times does the body of a REPEAT n TIMES loop run?

    The body of a REPEAT n TIMES loop runs exactly n times.

  • True or False?

    The body of a REPEAT UNTIL loop always runs at least once.

    False.

    If the condition is already true when the loop starts, the body does not execute at all.

  • What is displayed when this code segment runs?

    t ← 0
    
    REPEAT 4 TIMES
    
    {
    
       t ← t + 5
    
    }
    
    DISPLAY(t)

    The code segment displays 20, because 5 is added to t 4 times.

  • What is displayed when this code segment runs?

    k ← 10
    
    REPEAT UNTIL(k < 4)
    
    {
    
       DISPLAY(k)
    
       k ← k - 3
    
    }

    The code segment displays 10 7 4; k then becomes 1, so k < 4 is true and the loop stops.

  • When does a REPEAT UNTIL loop terminate?

    A REPEAT UNTIL loop terminates when its condition evaluates to true.

  • Define infinite loop.

    An infinite loop occurs when the condition of a REPEAT UNTIL loop never becomes true, so the loop runs indefinitely.

  • Name two common causes of an infinite loop.

    Common causes are the condition variable never being updated inside the loop body and a condition that is logically impossible to satisfy.

  • Every loop must have a clear, _________ stopping condition.

    Every loop must have a clear, reachable stopping condition.

  • Will this loop ever stop?

    x ← 5
    
    REPEAT UNTIL(x = 0)
    
    {
    
       x ← x + 2
    
    }

    This loop never stops: x increases by 2 each iteration, so it never equals 0 and the loop is infinite.

Sign up to unlock flashcards

or