Glossary
Execution Order
The specific sequence in which statements within nested loops are processed. The inner loop finishes all its cycles before the outer loop advances to its next iteration.
Example:
If an outer loop runs 3 times and an inner loop runs 2 times, the execution order ensures the inner loop completes twice for each of the outer loop's three iterations.
Inner Loop
The loop that is contained within another loop. It executes completely for every single iteration of its enclosing outer loop.
Example:
If you're searching for a specific value in a 2D array, the inner loop would iterate through the elements of a single row, while the outer loop moves to the next row.
Nested Loop
A programming construct where one loop is placed inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop.
Example:
To print a 5x5 grid of asterisks, you would use a nested loop where the outer loop handles rows and the inner loop handles columns.
Outer Loop
In a nested loop structure, this is the enclosing loop that controls the primary iterations. Its completion dictates how many times the inner loop will run in its entirety.
Example:
When generating a multiplication table, the outer loop might iterate from 1 to 10, representing the first factor in each multiplication problem.
Time Complexity
A measure of how the runtime of an algorithm scales with the size of its input, often expressed using Big O notation. For nested loops, it's typically the product of the iterations of each loop.
Example:
An algorithm with two nested loops, each iterating 'n' times, will have a time complexity of O(n^2), indicating its execution time grows quadratically with input size.
Tracing Code
The methodical process of manually stepping through a program's execution, line by line, to track the values of variables and predict the exact output.
Example:
To understand the output of a complex nested loop, a student might perform tracing code by creating a table to record the values of loop variables and printed results at each step.
break
A control flow statement that immediately terminates the innermost loop it is currently executing, transferring program control to the statement immediately following that loop.
Example:
When searching for the first occurrence of a number in a list, you can use break once the number is found to stop further iterations and exit the loop.
continue
A control flow statement that skips the remaining statements in the current iteration of the innermost loop and proceeds directly to the next iteration of that same loop.
Example:
If you are processing a list of numbers and want to skip any negative values, you can use continue when a negative number is encountered to move to the next number without processing the current one.