SMF For Free Support Forum
Signup For Free Forum
July 05, 2008, 06:26:19 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to SMF For Free. The best free SMF Host
 
   Home   Help Search Arcade Gallery Login Register  

Pages: [1]
  Print  
Author Topic: Java Loop Explained  (Read 263 times)
0 Members and 1 Guest are viewing this topic.
zilchuary
SMF For Free Newbie
*
Offline Offline

Posts: 11


View Profile
« on: January 04, 2008, 01:22:05 pm »

Code:
for ( i=1; i<6; i++ ) {
    System.out.println(i);
}

Well wouldnt you know
back to good old SMF Forums!


Lets begin

In this example, the loop starts with an initialization of the (int) variable i to the value 1. Each time the loop iteration starts, a the exit condition is evaluated (is i less than 6?) and if true, continues. When the loop iteration is complete, the incrementor code is executed (i++ means increment the variable i by one). The loop continues until the exit condition is met (when i is equal to or greater than 6), so the code block will be executed for i values of 1, 2, 3, 4, and 5.
The exit condition code can be more complex as needed because it is computed each time through the loop, so the condition can be based on variables which may change during the loop iteration. To run a code block on each value of an array, the loop condition could be i < array.length.
The incrementor code can also be more complex, essentially any valid Java code. Another simple example would be a decrementor using i-- which would subtract one from the variable i each time through the loop.

Logged


Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC
ServerBeach Coupon
Page created in 0.344 seconds with 18 queries.