Han

Han Ferik

Home | Projects | AP CSA

AP CSA Unit 4: Iteration

4.1:

- Iteration statements (loops) change the flow of control by repeating a set of statements zero or more times until a condition is met.

- Loops often have a loop control variable that is used in the Boolean condition, requiring the steps of initializing, testing, and changing the loop variable.

- In while loops, the Boolean expression is evaluated before each iteration, and the loop body executes as long as the condition is true, stopping when the condition is false.

- If the Boolean expression in a while loop evaluates to false initially, the loop body is not executed at all.

- Infinite loops occur when the Boolean expression always evaluates to true, causing the loop to never end.

- Off-by-one errors happen when the iteration statement loops one time too many or too few.

- Input-controlled loops often use a sentinel value (e.g., "bye" or -1) provided by the user to signal the loop to stop, though these are not on the AP CSA exam.

- Standard algorithms like computing a sum or average are commonly implemented with loops.

Example of a while loop:


        public class WhileExample {
            public static void main(String[] args) {
                int i = 0;
                while (i < 5) {
                    System.out.println("Iteration " + i);
                    i++; // Increment to avoid infinite loop
                }
            }
        }
        

4.2:

- In a for loop, the initialization statement is only executed once before the evaluation of the test Boolean expression. The variable being initialized is referred to as a loop control variable.

- In each iteration of a for loop, the increment or decrement statement is executed after the entire loop body is executed and before the Boolean expression is evaluated again.

- A for loop can be rewritten into an equivalent while loop and vice versa.

Example of a for loop:


        public class ForExample {
            public static void main(String[] args) {
                for (int i = 0; i < 5; i++) {
                    System.out.println("Iteration " + i);
                }
            }
        }
        

4.3:

- Loops can be used to traverse or process a string.

- Standard algorithms using string traversals can determine if one or more substrings have a particular property.

- Loops can calculate the number of substrings that meet specific criteria.

- String traversals can create a new string with the characters reversed.

Example of string traversal:


        public class StringTraversal {
            public static void main(String[] args) {
                String str = "hello";
                for (int i = 0; i < str.length(); i++) {
                    System.out.println("Character at index " + i + ": " + str.charAt(i));
                }
            }
        }
        

4.4:

- 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.

Example of nested loops:


        public class NestedLoopExample {
            public static void main(String[] args) {
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 2; j++) {
                        System.out.println("Outer loop: " + i + ", Inner loop: " + j);
                    }
                }
            }
        }
        

4.5:

- A trace table can be used to keep track of the variables and their values throughout each iteration of the loop.

- We can determine the number of times a code segment will execute with a statement execution count. This is called run-time analysis.

- The number of times a loop executes can be calculated by largestValue - smallestValue + 1, where these are the largest and smallest values of the loop counter variable possible in the body of the loop.

- The number of times a nested for-loop runs is the number of times the outer loop runs times the number of times the inner loop runs.

Example of calculating the number of executions:


        public class LoopExecutionCount {
            public static void main(String[] args) {
                int outerLoopCount = 3;
                int innerLoopCount = 2;
                System.out.println("Total iterations: " + (outerLoopCount * innerLoopCount));
            }
        }
        

Find me on the interwebs!

Github LinkedIn Instagram Facebook