Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loops

 
Bob Holland
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there, how do i use a for loop, and what is the correct syntax please?? Bob
 
Keith Biddlecomb
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (int i = 0; i < 5; i++) {
//your code goes here
}

This loop will repeat itself 5 times. I'm not sure how familiar you are with the i++ syntax, but it is the same as i = i + 1, so if you feel more comfortable writing that in the loop instead, it would also work. Just a word of caution, make sure, in general, you DO NOT redefine i (or whatever variable you choose to use as the counter) inside the loop. That is a very easy way to cause an endless loop.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your signature indicates you are in the Computer Science department at the University of Virginia, I assume you are learning Java as part of your cirriculum. If this is the case, you should be able to find explanations about Java syntax in your text book, so READ IT! If you don't find your textbook very clear on a particular topic, perhaps you can post a quote here so we can clarify what the author probably means.

If you are just learning Java on your own, rather than in a class currently, or if you want additional resources to help you learn the language, I would suggest that you check out Sun's Java Tutorial. It has some great lessons to get you up to speed on the basic syntax of the language.

Keep Coding!

Layne
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
Since your signature indicates you are in the Computer Science department at the University of Virginia ... you should be able to find explanations about Java syntax in your text book, so READ IT! ...

where "..." = (snip).

That's Keith's signature, who posted a reply to the original question from Bob.

(Just want to be fair to Keith. )
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

where "..." = (snip).

That's Keith's signature, who posted a reply to the original question from Bob.

(Just want to be fair to Keith. )

Doh! :roll: Apparently I need to read more carefully myself. My apologies Keith.

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic