Convert the following segment into an equivalent do- while loop.
int a, b;
for (a = 10, b = 20; b >= 10; b = b - 2) {
a++;
}
Single Choice
Views 86
Answer:
int a = 10, b = 20; do { a++; b = b - 2; } while (b >= 10);
Explanation:
Initialization: The initialization part a = 10, b = 20 is executed before the do-while loop.
Loop body: The statement a++ is executed inside the loop body.
Update: The decrement of b (b = b - 2) is moved inside the loop body, after a++.
Condition: The while (b >= 10) condition is checked at the end, ensuring it’s executed at least once, like the for loop.
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.