Page 1 of 1

## Looping Basic concept, nice for beginners Rate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=59169&amp;s=d16f56f118bacff146f66f5f884c22d7&md5check=' + ipb.vars['secure_hash'], cur_rating: 4, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

Posted 30 July 2008 - 07:48 PM

POPULAR

Ok, Looping...so it's basically doing something while a certain condition is true. That sentence is basically one of the loops forms.

The for Loop

Ok, so the for loop. This is a useful looping structure for traversing through arrays and such. Let's take a look at the syntax shall we?

```for (int x = 0; x < 100; x++)
{
// statements to execute 100 times
}
```

The for line, let's take a closer look at it...It starts with the for keyword of course, to signify that a loop follows. In the parentheses we have the loop control variable, x, which starts at zero (the first part, before the semicolon). The second part is what to stop at, in this case, 99, since x cannot be greater than or equal to 100. The third part is the value to increment by every time the end of the loop iteration is reached, in this case our shortcut is used for add 1 every time.

The while Loop

The while loop. A loop to continue going around...and around, and around until a certain condition is reached. Let's take a look at the syntax for this one.

```int x = 5;

while (x < 10)
{
System.out.println(x);
x++;
}
```

Well, not much to explain here, just put in the parentheses after while whatever you want the condition to stop, to be.

The do-while Loop

The do-while. This is much the same as the while loop, with only 1 difference. It MUST execute at least once, as it doesn't know what the condition to stop is, until after it executes the code once.

```int number = 10;

do
{
System.out.println(number);
number += 10;
}
while (number <= 100);
```

Well, there's one major difference between the while line in the regular while loop, and this one. Notice the semicolon, as this must be there for a do-while loop, as it signifies the end of the statement of the do-while loop. In other words, it just signifies the end.

_____________________________________________________________________

Modification Section:

The for each Loop

NOTE: To use this loop you must have JDK 1.5 or later, as that was when this loop was created.

Well, this is useful for traversing arrays in a different manner. It just goes for the entire length of the array, or ArrayList.

```// our 'int' array, NUMBERS, is pre-defined

for (int number : NUMBERS)
System.out.println(number);
```

The basic syntax for this is for (variable_to_use_inside_the_loop : array_to_look_in). It's the same as doing a for (int x = 0; x < NUMBERS.length; x++).
_____________________________________________________________________

This has been my basic introduction to java looping. It's not a very in-depth tutorial, but it's not supposed to be. Then again, looping is a fairly routine and easy thing to accomplish.

Hope this helped!

This post has been edited by Locke37: 01 August 2008 - 10:40 PM

Is This A Good Question/Topic? 5

## Replies To: Looping

### #2 abgorn

• sudo apt-get install brain

Reputation: 30
• Posts: 1,410
• Joined: 05-June 08

Posted 02 August 2008 - 03:43 PM

Nice, thanks for the good tutorial.

I constantly forget stuff about loops

### #3 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

Posted 03 August 2008 - 03:09 PM

Thanks for the feedback!

### #4 Dexy

Reputation: 0
• Posts: 1
• Joined: 07-August 08

Posted 07 August 2008 - 04:16 PM

great thanks !! but can u show us nested loops
like 2 for loops !!??
thanks !!

### #5 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

Posted 07 August 2008 - 04:20 PM

Oh! GOOD CALL

I'll edit it right now, while I'm thinking about it.

EDIT: Crap, it's too old to edit, but I've got a plan B.

This post has been edited by Locke37: 07 August 2008 - 04:22 PM

### #6 chili5

• D.I.C Lover

Reputation: 20
• Posts: 1,144
• Joined: 28-December 07

Posted 28 September 2008 - 11:56 AM

Great tutorial.

This is new to me:

```for (int number : NUMBERS)

```

I didn't know you can do that. I'll show that to my teacher tomorrow. Bookmarks page.

### #7 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

Posted 01 October 2008 - 08:38 AM

chili5, on 28 Sep, 2008 - 11:56 AM, said:

Great tutorial.

This is new to me:

```for (int number : NUMBERS)

```

I didn't know you can do that. I'll show that to my teacher tomorrow. Bookmarks page.

Yup, you can do that as of Java 5 (JDK 1.5).

Just make sure your school has that or 6!

### #8 amber.bosch

Reputation: 0
• Posts: 15
• Joined: 05-October 08

Posted 08 October 2008 - 01:23 PM

Locke37, on 30 Jul, 2008 - 07:48 PM, said:

Ok, Looping...so it's basically doing something while a certain condition is true. That sentence is basically one of the loops forms.

The for Loop

Ok, so the for loop. This is a useful looping structure for traversing through arrays and such. Let's take a look at the syntax shall we?

```for (int x = 0; x < 100; x++)
{
// statements to execute 100 times
}
```

The for line, let's take a closer look at it...It starts with the for keyword of course, to signify that a loop follows. In the parentheses we have the loop control variable, x, which starts at zero (the first part, before the semicolon). The second part is what to stop at, in this case, 99, since x cannot be greater than or equal to 100. The third part is the value to increment by every time the end of the loop iteration is reached, in this case our shortcut is used for add 1 every time.

The while Loop

The while loop. A loop to continue going around...and around, and around until a certain condition is reached. Let's take a look at the syntax for this one.

```int x = 5;

while (x < 10)
{
System.out.println(x);
x++;
}
```

Well, not much to explain here, just put in the parentheses after while whatever you want the condition to stop, to be.

The do-while Loop

The do-while. This is much the same as the while loop, with only 1 difference. It MUST execute at least once, as it doesn't know what the condition to stop is, until after it executes the code once.

```int number = 10;

do
{
System.out.println(number);
number += 10;
}
while (number <= 100);
```

Well, there's one major difference between the while line in the regular while loop, and this one. Notice the semicolon, as this must be there for a do-while loop, as it signifies the end of the statement of the do-while loop. In other words, it just signifies the end.

_____________________________________________________________________

Modification Section:

The for each Loop

NOTE: To use this loop you must have JDK 1.5 or later, as that was when this loop was created.

Well, this is useful for traversing arrays in a different manner. It just goes for the entire length of the array, or ArrayList.

```// our 'int' array, NUMBERS, is pre-defined

for (int number : NUMBERS)
System.out.println(number);
```

The basic syntax for this is for (variable_to_use_inside_the_loop : array_to_look_in). It's the same as doing a for (int x = 0; x < NUMBERS.length; x++).
_____________________________________________________________________

This has been my basic introduction to java looping. It's not a very in-depth tutorial, but it's not supposed to be. Then again, looping is a fairly routine and easy thing to accomplish.

Hope this helped!

I really like this but I noticed you are missing a few things. Could you redo a more detailed version, including if then else and nested loops? You would be the BEST!!

### #9 jv44heinzbar

Reputation: 0
• Posts: 26
• Joined: 24-February 09

Posted 23 March 2009 - 10:21 AM

Bump for nested loops!

HB

### #10 chili5

• D.I.C Lover

Reputation: 20
• Posts: 1,144
• Joined: 28-December 07

Posted 23 March 2009 - 01:09 PM

Why?

A nested loop is just a loop in a loop.

```for(int i=0;i<5;i++) {
for (int x=0;x<5;x++) {

}
}

```

Basically the inner loop executes 5 times for each execution of the outer loop. Not much to write another tutorial about. So the inner loop executes 25 times.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }