1/28
Flashcards covering AP Computer Science Principles Unit 4: JavaScript Control Structures (Lessons 4.1 to 4.12), including Boolean logic, operators, conditionals, event-driven programming, loops, random numbers, and tracing rules.
What is the primary function of control structures, and how would programs execute without them?
Control structures dictate the flow of execution in a program, allowing programs to make decisions (branching), handle real-time user input (events), and repeat tasks efficiently (iteration). Without control structures, computers could only execute code sequentially from top to bottom.
What are the five steps of the code-tracing routine for analyzing loops and conditionals?
true or false, exiting immediately if false); 3. Execute (run code lines inside the block line-by-line); 4. Update (execute counter modifications such as i++ or variable assignments); 5. Re-evaluate (return to Step 2 and test the boundary condition again).According to the critical exam rule, when does the loop update step (e.g., i++) execute?
The loop update step runs after the loop body executes, but before the condition is tested again.
What is a Boolean data type, and how does it relate to computer hardware?
A Boolean is a fundamental data type representing logic with exactly two values: true or false. Computers process information using binary switches (bits: 0 or 1), and Booleans are the high-level programming representation of these binary logic states.
How does JavaScript distinguish between a Boolean primitive and a string literal containing "true"?
true without quotes is a Boolean primitive data type (typeof returns "boolean"), which operates logically in conditions. "true" with quotes is a string (typeof returns "string"), which represents text and does not behave logically.
What is the evaluation rule for the logical AND (&&) operator?
The && operator evaluates to true only if both operands are true. If either operand is false, the entire expression evaluates to false.
What is the evaluation rule for the logical OR (||) operator?
The || operator evaluates to true if at least one operand is true. It evaluates to false only when both operands are false.
What does the logical NOT (!) operator do to a Boolean value?
The ! operator inverts the Boolean value, flipping true to false and false to true (for example, !(5 > 2) evaluates to false).
What are the two equivalent formulations established by De Morgan's Laws?
!(A && B) is logically equivalent to !A || !B, and !(A || B) is logically equivalent to !A && !B.
How does short-circuit logic evaluation work for && and || in JavaScript?
In (A && B), if A is false, JavaScript skips evaluating B because false && anything is always false. In (A || B), if A is true, JavaScript skips evaluating B because true || anything is always true.
What is identified as the "#1 programming error" regarding assignment and comparison?
Confusing = with ==. = is the assignment operator that sets a variable, whereas == is the comparison operator that tests if two values match. Writing if (x = 5) overwrites x with 5 instead of testing it.
What is the critical execution rule for an if / else if / else conditional chain?
The computer evaluates conditions sequentially from top to bottom. As soon as ONE branch evaluates to true, its code runs, and the entire rest of the chain is skipped.
How does the execution of multiple independent if statements differ from a chained else if structure?
With independent if statements, every condition is evaluated separately, meaning multiple blocks can execute if multiple conditions are true. In a chained else if structure, branches are mutually exclusive; once a condition evaluates to true, all subsequent branches are skipped.
How does event-driven programming differ from traditional procedural programming?
Traditional programs execute top-to-bottom sequentially (procedural flow). Event-driven programming pauses execution and waits asynchronously for user actions—such as key presses or mouse clicks—to trigger specific callback functions called event handlers.
What are the four key components of an event handler system, and what does e.keyCode represent?
e, an object containing event data where e.keyCode holds the numeric ID of the pressed key); 3. Event Handler (function designated to run on the event); 4. Condition (filters specific key codes, e.g., Keyboard.LEFT).What are the three components of a standard JavaScript for loop header, and what is their execution timing?
var i = 0): executes exactly once before the loop starts to declare the counter; 2. Condition (e.g., i < 5): tested before every iteration to decide if the body runs; 3. Update (e.g., i++): executes after the body finishes, right before re-testing the condition.Why do loop counters commonly start at 0, and how many times does for (var i = 0; i < N; i++) run?
Zero represents the first offset index position in computer memory. Starting at 0 with the condition i < N causes the loop to execute exactly N times.
What are the fast formulas for calculating the number of loop iterations for step size 1 using < versus <=?
For for (var i = A; i < B; i++), the loop executes (B−A) times. For for (var i = A; i <= B; i++), the loop executes (B−A+1) times.
What is the AP CSP range rule for Randomizer.nextInt(low, high)?
Randomizer.nextInt(min, max) is inclusive of both min and max. For example, Randomizer.nextInt(1, 6) generates any integer from the set {1,2,3,4,5,6} with equal probability.
What is the definition of a while loop, and when is it preferred over a for loop?
A while loop is an indefinite iteration structure that repeats as long as its Boolean condition remains true. It is preferred when you cannot predict in advance how many iterations are needed.
What causes an infinite loop bug in a while loop?
An infinite loop occurs if the code inside the while loop never modifies variables in a way that causes the condition to evaluate to false.
What is the operational difference between the break and continue keywords?
break exits the nearest enclosing loop immediately, jumping to the code directly after the loop. continue skips the remainder of the current iteration and jumps directly to the update step and condition test.
What is a "loop and a half" pattern, and how is it typically structured in code?
A loop and a half is a pattern used when a program needs to check an exit condition in the middle or at the end of a loop body. It is structured using an infinite loop header (while (true)), performing an action, performing a mid-loop exit check using break, and executing remainder code that only runs if not exiting.
What does the modulus operator (%) calculate, and what are its three common use cases?
The modulus operator returns the remainder of integer division. Its three common use cases are: 1. Even/Odd testing (N % 2 == 0 is even; N % 2 != 0 is odd); 2. Divisibility testing (N % K == 0 means N is divisible by K); 3. Clock/Cycling math (hour % 12 keeps numbers constrained inside a cyclic range).
How do nested loops execute, and how is the total count of inner loop executions determined?
In a nested loop, the inner loop completes all of its iterations for every single iteration of the outer loop. The total number of executions of the inner body equals the number of outer iterations multiplied by the number of inner iterations (outer×inner).
What is the evaluated value of var res = 17 % 5 + 2 * 3; and why?
8. Multiplicative and modulus operators precede addition: 17 % 5 equals 2, and 2 * 3 equals 6. Finally, 2+6=8.
How many times does println("AP"); print in for (var i = 2; i <= 10; i += 3)?
3 times. Tracing values of i: i=2 (prints), i=5 (prints), i=8 (prints). When i=11, the condition 11≤10 evaluates to false, exiting the loop.
Given var x = 10; var y = 20;, what is printed by if (x > 5 || y < 10 && x == 0) { println("Alpha"); } else { println("Beta"); }?
"Alpha". Because (x > 5) is true, the logical OR (||) short-circuits the rest of the conditional expression, causing the whole condition to evaluate to true.
What is printed by this loop trace: var a = 3; var b = 10; while (a < b) { a += 2; b -= 1; } println(a + ", " + b);?
"9, 7". Trace steps: Round 1 (3<10): a=5,b=9; Round 2 (5<9): a=7,b=8; Round 3 (7<8): a=9,b=7; Round 4 check (9<7): false, loop terminates.