Answers Already Passed
What is the purpose of the `break` statement in a loop?
✔✔ It immediately exits the loop.
What does the `continue` statement do inside a loop?
✔✔ It skips the current iteration and moves to the next one.
Can a `for` loop run indefinitely?
✔✔ Yes, by omitting the condition or using `true` as the condition.
What happens if you forget to update the loop control variable in a `for` loop?
✔✔ The loop may run infinitely if the condition never changes.
How do you exit a loop based on user input?
✔✔ By checking the input inside the loop and using `break` if necessary.
1
,What is the purpose of nested loops?
✔✔ To iterate over multiple levels of data, such as a table or grid.
Can you nest different types of loops?
✔✔ Yes, a `for` loop can be inside a `while` loop, and vice versa.
How do you calculate the total of numbers entered by a user using a loop?
✔✔ By initializing a sum variable and adding user inputs in each iteration.
What happens when a `break` statement is used inside a nested loop?
✔✔ It exits only the innermost loop where it is placed.
What is a loop in Java?
✔✔ A control structure that repeats a block of code multiple times.
What are the three main types of loops in Java?
✔✔ `for`, `while`, and `do-while`.
2
, How does a `for` loop work in Java?
✔✔ It runs a block of code a specific number of times based on an initialization, condition, and
update statement.
What is the primary difference between a `while` loop and a `do-while` loop?
✔✔ A `while` loop checks the condition before executing, while a `do-while` loop executes at
least once before checking the condition.
When should you use a `while` loop instead of a `for` loop?
✔✔ When the number of iterations is unknown and depends on a condition being met.
What is an infinite loop?
✔✔ A loop that runs endlessly because its condition never becomes false.
How can you prevent an infinite loop?
✔✔ By ensuring the loop condition will eventually be false.
What happens if the condition in a `while` loop is initially false?
3