0

I would like to ask you guys a question. You see, I know what a for-loop is for but can someone please maybe explain how one works, just to help me get my head around it, an example is:

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

Now obviously that will just print Hello 10 times into the console but that's besides the point, I want to know how the for-loop works.

Sorry if i have confused anyone asking this - Shaun

4
  • 2
    #1 resource: download.oracle.com/javase/tutorial/java/nutsandbolts/for.html Commented Nov 5, 2011 at 23:11
  • I'm not sure what you are asking. Iteration is a central concept to computer programming. How would you examine a list of ten million items, for example? Commented Nov 5, 2011 at 23:13
  • @JustinSatyr: Have a look at Ted Hopp's answer, below. Commented Nov 5, 2011 at 23:22
  • @GregS - I think OP is asking about the semantics of the for construct in Java, not about iteration as a concept. Commented Nov 6, 2011 at 1:15

3 Answers 3

10

The for loop in your example is more or less equivalent to this:

int i = 0; while (i < 10) { System.out.println("hello"); i++; } 

The only difference is that with your for loop, the variable i exists only within the scope of the loop.

Every for loop can be transformed into a while loop using this same pattern.

for (init; test; continuation) { // loop body } 

becomes:

init; while (test) { // loop body continuation; } 

Again, the only difference will be with the scope of any variables declared in init.

Sign up to request clarification or add additional context in comments.

2 Comments

It's not the only difference. If in the for loop you use continue;, i will be incremented, whereas in the while "equivalent" it will not.
@ArmenTsirunyan - Thanks for pointing this out. The continue statement violates the rules of structured programming. (So do return, throws, and break.) It is hard to model it with a while loop. When I used "only", I had in mind OP's specific example; I should have been more careful in my word choice.
3

The for Statement

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) { statement(s) } 

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it's executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

1 Comment

Thanks a lot man, i love how simple java is when you know it haha :D
0

Well, this is how it is set up:

for (a; b; c)

"A" is something that is done at the beginning of the loop. It can actually be left out if necessary, like this:

for (; b; c)

"B" must be a true or false statement (like i<10, it either is or it isn't). Once "b" is no longer true, the loop stops.

"C" is something that is done at the end of the loop.

3 Comments

Actually, any or all of a, b, and c can be left out of a for loop.
I guess you're right, but wouldn't leaving out b make it infinite?
If you leave out b, then it is indeed an infinite loop. The only ways out are break, return or throwing an exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.