Difference between revisions of "For Tutorial"

From Progzoo
Jump to: navigation, search
Line 11: Line 11:
 
  i<10
 
  i<10
 
:Iteration - this happens after each step.
 
:Iteration - this happens after each step.
 +
i++
 
:Body - this is the bit that gets executed each time, the body is alway indented
 
:Body - this is the bit that gets executed each time, the body is alway indented
 
  System.out.println(i);
 
  System.out.println(i);
  
 
[[Category:Tutorial]]
 
[[Category:Tutorial]]

Revision as of 18:58, 24 September 2008

You can use the for loop to make a section of code repeat many times.

for (int i=0;i<10;i++)
  System.out.println(i);

The line System.out.println(i); happens 10 times.

There are four parts to the structure:

Initialization happens once at the start of the loop
int i=0;
Test - the loop executes only if the test is passed.
i<10
Iteration - this happens after each step.
i++
Body - this is the bit that gets executed each time, the body is alway indented
System.out.println(i);