This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop

 
jenda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello!

Can some one tell me how to use a for loop
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The Java Tutorial at Sun can.
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically a for loop lets you step through a certain action or set of actions a set number of times.
It generally has the signature:

for (int i=0; i<10; i++) {

}

which means proceed though this action 10 times.

int i=0 set the initial value to 0
i<10 checks that the value is still under 10
i++ adds one to i and the whole thing goes round again until step 2 is no longer true then it exits.

for (int i=0; i<10; i++) {
System.out.println("i now equals : " + i);
}

Try putting this code into a main and running it/changing the values around.

P.S This is only a very basic description .. there are many different permutations of this basic form which will add different functionality.
You're best bet is to get a good Java book and check through the basics
Have a look at Thinking in Java (it's a free download)


Bruce eckel's site
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jenda,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We request that display names follow the pattern FIRST_NAME + SPACE + LAST_NAME.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Dirk Schreckmann
Sheriff
Posts: 7023
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic