Java Loops Explained: while, do-while, for, for-each - Quiz
Total: 8 questions
1. How many logical expression a 'for' conditional expression can have?
How many logical expression a 'for' conditional expression can have?
Only one, but it can be complex.
2. Parts of basic for statement.
Parts of basic for statement.
It has three parts: declaration and/or initialization, boolean evaluation, and the iteration expression.
3. What is the difference between the break and continue statements in a Java loop?
What is the difference between the break and continue statements in a Java loop?
break terminates the loop immediately, and execution continues with the first line after the loop. continue skips only the rest of the current iteration and jumps straight to the next condition check, so the loop keeps running. In nested loops, both statements affect only the loop they are directly placed in; to leave several levels at once you use a labeled break.
4. What is the for-each (enhanced for) loop used for, and what are its limitations?
What is the for-each (enhanced for) loop used for, and what are its limitations?
The for-each loop (introduced in Java 5) iterates sequentially over the elements of an array or any object that implements Iterable. Its syntax is for (type variable : arrayOrCollection) { ... }, and it is shorter and safer than a regular for loop because you cannot make an off-by-one index mistake. Its limitations: it gives you no access to the element's index, assigning a new value to the iteration variable does not change the array or collection, and you cannot easily traverse only part of the elements or use a custom step. When you need the index, need to replace elements, or need a non-standard step, use a regular for loop with a counter.
5. How do you decide which loop to use in Java: while, do-while, for, or for-each?
How do you decide which loop to use in Java: while, do-while, for, or for-each?
Use while when the number of repetitions is not known in advance and the condition must be checked before the body (the body may run zero times). Use do-while when the body must run at least once, for example when prompting the user for input, since its condition is checked after the body. Use for when the number of repetitions is known in advance or you need a counter/index. Use for-each when you simply need to visit every element of an array or a collection and do not need the index or to modify elements.
6. What is the difference between the break and continue statements in a Java loop?
What is the difference between the break and continue statements in a Java loop?
break terminates the loop immediately, and execution continues with the first line after the loop. continue skips only the rest of the current iteration and jumps straight to the next condition check, so the loop keeps running. In nested loops, both statements affect only the loop they are directly placed in; to leave several levels at once you use a labeled break.
7. What is the for-each (enhanced for) loop used for, and what are its limitations?
What is the for-each (enhanced for) loop used for, and what are its limitations?
The for-each loop (introduced in Java 5) iterates sequentially over the elements of an array or any object that implements Iterable. Its syntax is for (type variable : arrayOrCollection) { ... }, and it is shorter and safer than a regular for loop because you cannot make an off-by-one index mistake. Its limitations: it gives you no access to the element's index, assigning a new value to the iteration variable does not change the array or collection, and you cannot easily traverse only part of the elements or use a custom step. When you need the index, need to replace elements, or need a non-standard step, use a regular for loop with a counter.
8. How do you decide which loop to use in Java: while, do-while, for, or for-each?
How do you decide which loop to use in Java: while, do-while, for, or for-each?
Use while when the number of repetitions is not known in advance and the condition must be checked before the body (the body may run zero times). Use do-while when the body must run at least once, for example when prompting the user for input, since its condition is checked after the body. Use for when the number of repetitions is known in advance or you need a counter/index. Use for-each when you simply need to visit every element of an array or a collection and do not need the index or to modify elements.