Hi,

Fairly new to java and programming. Could anyone explain the difference between a for loop and a do while loop in relation to writing a program and typing in numbers and displaying the total.

Thanks

Recommended Answers

All 3 Replies

A for-loop is typically used when the number of passes through the loop is known in advance.
A while loop is typically used when the number of passes is NOT known in advance.
A do-while loop is rarely used but can be useful if the loop is known to have to run at least once and the total number of runs isn't known (or at least not known before the end of that first run).

A for-loop is typically used when the number of passes through the loop is known in advance.
A while loop is typically used when the number of passes is NOT known in advance.
A do-while loop is rarely used but can be useful if the loop is known to have to run at least once and the total number of runs isn't known (or at least not known before the end of that first run).

yea just want to add on. which loop you use depends on what you want to do. As for while and do while loop , the concept is almost the same, it depends on ur preference.

Hi,

Fairly new to java and programming. Could anyone explain the difference between a for loop and a do while loop in relation to writing a program and typing in numbers and displaying the total.

Thanks

for loop is evaluate up to the condition is false.

whereas

do-while loop is evaluate once eventhough the condition is false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.