Unit 2 · Selection and Iteration
● Core concept · ○ Supporting concept
2.1 Algorithms with Selection and Repetition
Building blocks of algorithms ● (core concept) — The building blocks of algorithms include sequencing, selection, and repetition.
Selection ● (core concept) — Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.
Repetition ● (core concept) — Repetition is when a process repeats itself until a desired outcome is reached.
Order of building blocks ● (core concept) — The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
2.2 Boolean Expressions
Relational operators == and != ● (core concept) — Values can be compared using the relational operators == and != to determine whether the values are the same. With primitive types, this compares the actual primitive values. With reference types, this compares the object references.
Relational operators <, >, <=, >= ● (core concept) — Numeric values can be compared using the relational operators <, >, <=, and >= to determine the relationship between the values.
Boolean expression (relational) ● (core concept) — An expression involving relational operators evaluates to a Boolean value.
2.3 if Statements
Selection statement ● (core concept) — Selection statements change the sequential execution of statements.
if statement ● (core concept) — An if statement is a type of selection statement that affects the flow of control by executing different segments of code based on the value of a Boolean expression.
One-way selection (if) ● (core concept) — A one-way selection (if statement) is used when there is a segment of code to execute under a certain condition. In this case, the body is executed only when the Boolean expression is true.
Two-way selection (if-else) ● (core concept) — A two-way selection (if-else statement) is used when there are two segments of code — one to be executed when the Boolean expression is true and another segment for when the Boolean expression is false. The body of the if is executed when the Boolean expression is true, and the body of the else is executed when the Boolean expression is false.
2.4 Nested if Statements
Nested if statements ● (core concept) — Nested if statements consist of if, if-else, or if-else-if statements within if, if-else, or if-else-if statements.
Inner nested if evaluation ● (core concept) — The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
Multiway selection (if-else-if) ● (core concept) — A multiway selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multiway selection is performed such that no more than one segment of code is executed, based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.
2.5 Compound Boolean Expressions
Logical operators ● (core concept) — Logical operators ! (not), && (and), and || (or) are used with Boolean expressions. The expression !a evaluates to true if a is false and evaluates to false otherwise. The expression a && b evaluates to true if both a and b are true and evaluates to false otherwise. The expression a || b evaluates to true if a is true, b is true, or both, and evaluates to false otherwise. The order of precedence for evaluating logical operators is ! (not), && (and), then || (or). An expression involving logical operators evaluates to a Boolean value.
Short-circuit evaluation ● (core concept) — Short-circuit evaluation occurs when the result of a logical operation using && or || can be determined by evaluating only the first Boolean expression. In this case, the second Boolean expression is not evaluated.
2.6 Comparing Boolean Expressions
Equivalent Boolean expressions ● (core concept) — Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent.
De Morgan's law ● (core concept) — De Morgan's law can be applied to Boolean expressions to create equivalent Boolean expressions. Under De Morgan's law, the Boolean expression !(a && b) is equivalent to !a || !b and the Boolean expression !(a || b) is equivalent to !a && !b.
Comparing object references ● (core concept) — Two different variables can hold references to the same object. Object references can be compared using == and !=.
Comparing a reference with null ● (core concept) — An object reference can be compared with null, using == or !=, to determine if the reference actually references an object.
equals method (class-defined) ● (core concept) — Classes often define their own equals method, which can be used to specify the criteria for equivalency for two objects of the class. The equivalency of two objects is most often determined using attributes from the two objects.
2.7 while Loops
Iteration ● (core concept) — Iteration is a form of repetition. Iteration statements change the flow of control by repeating a segment of code zero or more times as long as the Boolean expression controlling the loop evaluates to true.
Infinite loop ● (core concept) — An infinite loop occurs when the Boolean expression in an iterative statement always evaluates to true.
Loop body not executing ● (core concept) — The loop body of an iterative statement will not execute if the Boolean expression initially evaluates to false.
Off-by-one error ● (core concept) — Off by one errors occur when the iteration statement loops one time too many or one time too few.
while loop ● (core concept) — A while loop is a type of iterative statement. In while loops, the Boolean expression is evaluated before each iteration of the loop body, including the first. When the expression evaluates to true, the loop body is executed. This continues until the Boolean expression evaluates to false, whereupon the iteration terminates.
2.8 for Loops
for loop ● (core concept) — A for loop is a type of iterative statement. There are three parts in a for loop header: the initialization, the Boolean expression, and the update.
Loop control variable ● (core concept) — In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable. The Boolean expression is evaluated immediately after the loop control variable is initialized and then following each execution of the update until it is false. In each iteration, the update is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
for/while loop equivalence ● (core concept) — A for loop can be rewritten into an equivalent while loop (and vice versa).
2.9 Implementing Selection and Iteration Algorithms
Standard selection and iteration algorithms ● (core concept) — Standard algorithms (without data structures) include: identifying if an integer is or is not evenly divisible by another integer; identifying the individual digits in an integer; determining the frequency with which a specific criterion is met; determining a minimum or maximum value; and computing a sum or average.
2.10 Implementing String Algorithms
Standard string algorithms ● (core concept) — Standard string algorithms include: finding if one or more substrings have a particular property; determining the number of substrings that meet specific criteria; and creating a new string with the characters reversed.
2.11 Nested Iteration
Nested iteration ● (core concept) — Nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue to its next iteration.
2.12 Informal Run-Time Analysis
Statement execution count ● (core concept) — A statement execution count indicates the number of times a statement is executed by the program. Statement execution counts are often calculated informally through tracing and analysis of the iterative statements.