Advertise Here

Author Topic: Java Loop Explained  (Read 19892 times)

0 Members and 1 Guest are viewing this topic.

Offline zilchuary

  • SMF For Free Newbie
  • *
  • Posts: 16
    • View Profile

  • Total Badges: 10
    Badges: (View All)
    Topic Starter Combination Level 2 Level 1 10 Posts
Java Loop Explained
« on: January 04, 2008, 01:22:05 pm »
Code: [Select]
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.


Offline ipwxy

  • SMF For Free Newbie
  • *
  • Posts: 9
    • View Profile

  • Total Badges: 6
    Badges: (View All)
    Windows User Topic Starter Level 1 First Post Second year Anniversary
Re: Java Loop Explained
« Reply #1 on: June 09, 2009, 08:54:25 pm »
You should have said 'for loop'. There are other loops rather than for, such as the 'while loop', and people can get confused.

Offline Steven

  • SMF For Free Full Member
  • *
  • Posts: 132
    • View Profile

  • Total Badges: 15
    Badges: (View All)
    Topic Starter Combination Level 3 Level 2 Level 1
Re: Java Loop Explained
« Reply #2 on: November 25, 2009, 05:41:18 pm »
In order to make this java loop topic more "complete" let's add the while, do while, and for each loop, break, and continue examples here as well =P

WHILE LOOP

Code: (java) [Select]
int i = 1;
while(i <= 5){
     System.out.println(i);
     i++;
}

Just like in the example above, this piece of code will print every number from 1 to 5. The thing about the While loop is that it is used mainly for boolean expressions (such as the above where the code will only execute as long as i is less than or equal to 5) but can also be used as follows:

Code: (java) [Select]
boolean a = true;
int i = 1;
while(a){
     if(i >= 5)
          a = false;
     System.out.println(i);
}

Now in this example, we check whether or not I is greater than or equal to 5. When it is equal to 5 we set a to false so that the loop will terminate on the next execution. Now let's take a look at do while.

DO WHILE

The point of a do while is to execute a while loop AT LEAST ONCE regardless of condition, because conditions are checked after the loops execution. This is generally useful when you receive user input that will determine the execution of a loop, but regardless of their input you would want the loop to run at least once. Here's an example of the do while loop, using the exact same example as our previous while loop.

Code: (java) [Select]
boolean a;
int i = 1;
do{
     if(i >= 5)
          a = false;
     System.out.println(i);
}while(a)

Notice how the while part is the last bit of the loop, this is why a would not have to be initialized until it becomes false.

FOR EACH


FOR EACH loops are used for iterating within arrays, it really makes it a lot easier, and more efficient, than using a regular for loop. Here is an example with an array of length 4, and you want to print each individual part of the array. I will show how it would look in your regular for loop, then in a foreach loop.

for:
Code: (java) [Select]
String array[] = {"Hi", "There", "My", "Son"}
for(int i = 0; i<array.length; i++)
     System.out.print(array[i]);

foreach:
Code: (java) [Select]
String array[] = {"Hi", "There", "My", "Son"}
foreach(String x : array)
     System.out.print(x);

you tell me which looks better ;)

BREAK AND CONTINUE

These two commands are quite crucial to loops, which is why I added them in here.

the "break" command breaks out of a loop, while the continue command skips the rest of the loop, and begins the iteration from the next step. For example, let's say you have a loop where you need the loop to terminate if a certain condition is met. All you would do is have an if statement using the break command and you'd be set! Example:

Code: (java) [Select]
if(condition)
     break;

as for the continue command would be the same, only it would be if you find something where you want the rest of the loop to be ignored, and then re-iterate as normal from the next iteration of the loop.

Code: (java) [Select]
if(condition)
     continue;

Hopefully this guide on loops is more complete for you guys ^^

Offline HSOwner

  • SMF For Free Newbie
  • *
  • Posts: 2
    • View Profile

  • Total Badges: 9
    Badges: (View All)
    Topic Starter Combination Level 2 Level 1 First Post
Re: Java Loop Explained
« Reply #3 on: December 17, 2009, 08:16:05 pm »
Nice

 

Related Topics

  Subject / Started by Replies Last post
4 Replies
6332 Views
Last post July 11, 2007, 12:40:04 pm
by doggiedoo86
3 Replies
9654 Views
Last post February 04, 2009, 07:44:58 pm
by nondier
1 Replies
4953 Views
Last post July 02, 2008, 01:16:07 pm
by Jessie
10 Replies
12296 Views
Last post January 09, 2010, 01:57:39 pm
by hell hound
1 Replies
1029 Views
Last post June 07, 2009, 09:33:51 am
by SMF For Free