1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Revise later
SpaceTo flip
If confident
All Flashcards
What are the key differences between for and while loops?
For loops are best for known iterations; while loops are for unknown iterations based on a condition.
Compare and contrast for loops and for-each loops.
For loops use an index; for-each loops iterate directly over elements. For-each are simpler but can't modify the original array directly.
Compare the use cases of for loops and do-while loops.
For loops are for definite iterations; do-while loops ensure at least one execution.
Compare using a for loop to iterate through an array vs. an ArrayList.
Arrays use `.length` and direct indexing. ArrayLists use `.size()` and `.get(index)`.
What are the similarities between for and while loops?
Both are used for repeated execution of a block of code based on a condition.
Compare the readability of a for loop versus a while loop for array traversal.
For loops are generally more concise and readable for simple array traversal.
Compare the use of break statements in for loops and while loops.
Both can use `break` to exit the loop prematurely.
Compare the use of continue statements in for loops and while loops.
Both can use `continue` to skip the current iteration.
Compare the control over loop variables in for loops and while loops.
For loops provide more structured control over initialization, condition, and increment/decrement.
Compare the common mistakes made with for loops and while loops.
For loops: off-by-one errors. While loops: forgetting to update the loop variable.
What is a for loop?
A control flow statement for specifying iteration, allowing code to be executed repeatedly.
What is initialization in a for loop?
The step that executes once at the beginning of the for loop.
What is the condition in a for loop?
A boolean expression that determines whether the loop will execute.
What is increment/decrement in a for loop?
The operation performed at the end of each loop iteration to update the loop variable.
Define iteration.
A single execution of the statements within a loop.
What is a loop variable?
A variable used to control the execution of a loop.
What is the purpose of a control structure?
To control the flow of execution in a program.
Define refactoring.
The process of restructuring existing computer codeโchanging the factoringโwithout changing its external behavior.
What is an infinite loop?
A loop that executes indefinitely because the condition is always true.
Define traversing (in the context of arrays).
Accessing each element of an array or data structure in a sequential manner.
When is a for loop most appropriate?
When you know the number of repetitions needed.
What are the three parts of a for loop's header?
Initialization, condition, and increment/decrement.
How do for loops relate to arrays?
For loops are commonly used to iterate through arrays.
Explain the purpose of the condition in a for loop.
It determines when the loop stops executing.
Why is it important to update variables inside a loop?
To avoid infinite loops or incorrect results.
What happens if the condition in a for loop is always true?
The loop will run indefinitely (infinite loop).
What is the relationship between loop variables and array indices?
Loop variables are often used as indices to access elements in an array.
Explain the concept of nested loops.
A loop inside another loop, often used for processing multi-dimensional arrays.
How can for loops be used to search for a specific element in an array?
By iterating through the array and checking each element against the target value.
How do you declare a for-each loop?
for (dataType item : arrayName) { // code block }