File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop" Watch "for loop" New topic

for loop


Joined: Dec 03, 2004
Posts: 1

Can some one tell me how to use a for loop
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Yes. The Java Tutorial at Sun can.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
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

Joined: Dec 10, 2001
Posts: 7023

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!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Here is a list of free on-line Java tutorials and books that I have found useful. Many of them cover using a for-loop.
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist
    I agree. Here's the link:
    subject: for loop
    It's not a secret anymore!