954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Loops

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

WillisTi
Newbie Poster
1 post since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

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).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
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.

starsunited
Light Poster
44 posts since Dec 2004
Reputation Points: 11
Solved Threads: 1
 

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.

bommavj
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You