• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

for loops

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there, how do i use a for loop, and what is the correct syntax please?? Bob
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic