0% found this document useful (0 votes)
8 views2 pages

While vs Do-While Loops in Java

The document explains the difference between while loops and do...while loops in Java. A while loop checks the condition before executing the loop body, while a do...while loop executes the body first and checks the condition afterward. Code examples illustrate the syntax and functionality of both loop types.

Uploaded by

dagimdemissew193
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

While vs Do-While Loops in Java

The document explains the difference between while loops and do...while loops in Java. A while loop checks the condition before executing the loop body, while a do...while loop executes the body first and checks the condition afterward. Code examples illustrate the syntax and functionality of both loop types.

Uploaded by

dagimdemissew193
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;

You might also like