OOP WORKSHEET
1. What is the main difference between a while loop and a do...while loop?
In Java, the main difference between a while loop and a do...while loop is the order of condition,
checking, and execution:
A. while Loop: Is the simplest of all the looping structures in java. It is an entry-controlled loop
statement which means the condition is checked first, before executing the loop body. If the
condition is false initially, the loop body never executes.
Syntax:
initialization;
while (condition) {
// body
}
Code example:
int sum = 0;
int number = 1;
int lastNumber = 5;
while (number <= lastNumber) {
sum += number;
number++;
}
[Link](“Sum=”+sum;
B. do...while Loop: It is an exit-controlled loop statement which means loop body executes first,
then the condition is checked. Even if the condition is false initially, the loop body executes at
least once.
Syntax:
do {
// body
} while (condition);
Code example:
int sum = 0;
int number = 1;
int lastNumber = 5;
do {
sum += number;
number++;
} while (number <= lastNumber);
[Link](“Sum=”+sum;