zuai-logo

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.

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 the output of the following code? ```java int sum = 0; for (int i = 0; i < 5; i++) { sum += i; } System.out.println(sum); ```
10
What is the output of the following code? ```java for (int i = 1; i <= 3; i++) { System.out.print(i + " "); } ```
1 2 3
What is the output of the following code? ```java int x = 5; for (int i = 0; i < x; i++) { System.out.print("*"); } ```
*****
What is the output of the following code? ```java int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } ```
1 2 3 4 5
What is the output of the following code? ```java int count = 0; for (int i = 0; i < 10; i += 2) { count++; } System.out.println(count); ```
5
Identify the error in the following code: ```java for (int i = 0; i < 10; ) { System.out.println(i); } ```
Missing increment/decrement statement, leading to an infinite loop.
What is the output of the following code? ```java int product = 1; for (int i = 1; i <= 4; i++) { product *= i; } System.out.println(product); ```
24
What is the output of the following code? ```java String str = "Hello"; for (int i = 0; i < str.length(); i++) { System.out.print(str.charAt(i) + " "); } ```
H e l l o
What is the output of the following code? ```java int[] nums = {5, 10, 15}; for (int num : nums) { System.out.print(num + " "); } ```
5 10 15
What is the output of the following code? ```java int result = 0; for (int i = 1; i <= 5; i++) { if (i % 2 != 0) { result += i; } } System.out.println(result); ```
9
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.