• Post Reply Bookmark Topic Watch Topic
  • New Topic

loops in java

 
casperl roh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for Loops
A for loop is used to repeat a statement until a condition is met.

for (initialization; test; increment) {
statement;
}

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.
Example
int a=1;
do {
a *= 3;
System.out.print(a + “ “);
} while (a < 30);
 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Query?
 
Darryl Burke
Bartender
Posts: 5162
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the cross post for this one:
http://www.javaprogrammingforums.com/loops-control-statements/10252-loop-java.html
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!