Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes loops in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loops in java" Watch "loops in java" New topic

loops in java

casperl roh

Joined: Aug 05, 2011
Posts: 8
for Loops
A for loop is used to repeat a statement until a condition is met.

for (initialization; test; increment) {

while Loops
The while loop repeats a statement for as long as a particular condition remains true.
Here’s an example:
while (a<3) {
x = x * a++; // the body of the loop

do-while Loops
The do loop is just like a while loop with one major difference—the place in the loop
when the condition is tested.
A while loop tests the condition before looping, so if the condition is false the first
time it is tested, the body of the loop never executes.
A do loop executes the body of the loop at least once before testing the condition, so if
the condition is false the first time it is tested, the body of the loop already will have
executed once.
int a=1;
do {
a *= 3;
System.out.print(a + “ “);
} while (a < 30);
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064


Mohamed Sanaulla | My Blog
Darryl Burke

Joined: May 03, 2008
Posts: 4523

Here's the cross post for this one:

luck, db
There are no new questions, but there may be new answers.
Don't get me started about those stupid light bulbs.
subject: loops in java
Similar Threads
Code using Loops
Cant understand programm
Unexpected output ??
A simple q. abt. LOOP
For loop Pyramid Program