zuai-logo

What are the differences between for and while loops?

For loop: Used for known number of iterations, initialization/condition/update in one line. While loop: Used for unknown number of iterations, condition checked before each iteration, update manual.

All Flashcards

What are the differences between for and while loops?
For loop: Used for known number of iterations, initialization/condition/update in one line. While loop: Used for unknown number of iterations, condition checked before each iteration, update manual.
What are the differences between single and nested loops?
Single loop: Iterates a block of code once for each value. Nested loop: An inner loop runs completely for each iteration of the outer loop.
What are the differences between print and println?
print: Prints output to the console without a newline. println: Prints output to the console with a newline.
What does the following code output? ```java for (int i = 0; i < 5; i++) { System.out.print(i + " "); } ```
0 1 2 3 4
What does the following code output? ```java for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } ```
* ** *** **** *****
How many times does the inner loop run? ```java for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 3; j++) { System.out.print(j + " "); } System.out.println(); } ```
15
What does the following code output? ```java int i = 1; while (i <= 5) { int j = 1; while (j <= i) { System.out.print(i + " "); j++; } System.out.println(); i++; } ```
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
What does the following code output? ```java int i = 1; while (i <= 5) { int j = 5; while (j >= i) { System.out.print("*"); j--; } System.out.println(); i++; } ```
***** **** *** ** *
What is the output of the following code? ```java int x = 5; int y = 10; if (x < y) { System.out.println("x is less than y"); } else { System.out.println("x is not less than y"); } ```
x is less than y
What is the output of the following code? ```java int sum = 0; for (int i = 1; i <= 3; i++) { sum += i; } System.out.println(sum); ```
6
What is the output of the following code? ```java int i = 0; while (i < 4) { System.out.print(i + " "); i++; } ```
0 1 2 3
Identify the error in the following code: ```java for (int i = 0; i < 10; i--) System.out.println(i); ```
The loop will run infinitely because `i` is decremented instead of incremented, so the condition `i < 10` will always be true.
What is the output of the following code? ```java int x = 10; int y = 5; System.out.println(x % y); ```
0
What is code tracing?
Manually executing code line by line, tracking variable values.
What is a tracing table?
A table used to organize code tracing, with columns for iteration, variables, and output.
What is a for loop?
A control flow statement for iterating a specific number of times.
What is a while loop?
A control flow statement that executes a block of code as long as a condition is true.
What is an iteration?
A single pass through a loop's code block.
What is initialization (in a loop)?
The process of setting the initial value of a loop counter variable.
What is the condition (in a loop)?
A boolean expression that determines whether a loop continues to execute.
What is the update (in a loop)?
The modification of a loop counter variable after each iteration.
What is a nested loop?
A loop inside another loop.
What is output (in code tracing)?
The values printed to the console by the code.