All Flashcards
What is a nested loop?
A loop inside another loop.
What is the outer loop in a nested loop?
The loop that controls the overall iterations.
What is the inner loop in a nested loop?
The loop that executes completely for each cycle of the outer loop.
What does the break statement do in a nested loop?
Exits the inner loop immediately.
What does the continue statement do in a nested loop?
Skips the current iteration of the inner loop.
What is the key difference between break and continue in nested loops?
break exits the loop entirely, while continue only skips the current iteration.
How many times does the inner loop execute if the outer loop runs 'n' times and the inner loop runs 'm' times?
n * m times.
What is the first step in the execution order of nested loops?
The outer loop starts.
What is the second step in the execution order of nested loops?
The inner loop starts and runs to completion.
What is the key to understanding the output of nested loops?
Pay close attention to how the loop variables change in both the inner and outer loops.
How do nested loops affect time complexity?
Nested loops often result in O(n^2) or worse time complexity.
What is the primary use case of nested loops?
Iterating over elements in 2D arrays or matrices.
What is the role of the outer loop in printing patterns?
Controls the number of rows.
What is the role of the inner loop in printing patterns?
Controls the elements (e.g., stars, numbers) printed in each row.
What is the impact of break on the outer loop?
The outer loop continues to the next iteration.
What is the impact of continue on the outer loop?
The outer loop continues to the next iteration.