# For Loops (Explain - Beginner)

Page 1 of 1

## 7 Replies - 1175 Views - Last Post: 21 September 2011 - 02:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248017&amp;s=a4e553b2296636ca43e44542de0e1383&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lmharmon

Reputation: 0
• Posts: 30
• Joined: 03-September 11

# For Loops (Explain - Beginner)

Posted 21 September 2011 - 06:20 AM

So, I'm fairly new to Java and I'm having a bit of trouble understanding for loops. Say you want a code that first sets a variable total to 0 and then uses the for loop to add 3 to total 17 times. I know the code is this:

```int total = 0;
for (int index = 1; index <= 17; index++)
{
total = total + 3;
}

```

...But I don't quite understand why. I understand that int total = 0, but not the rest of it. ?

Is This A Good Question/Topic? 0

## Replies To: For Loops (Explain - Beginner)

### #2 creative code monkey

Reputation: 14
• Posts: 45
• Joined: 14-September 11

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 06:25 AM

You can think of it in terms of this:

There are three main components to the 'for' loop, each of them separated by a ';'. The first part, 'int index = 1' sets up a standard int variable called 'index' and sets it to 1. Pretty simple. The second part, 'index <= 17, simply means 'iterate over the code within this for loop, while index is less than or equal to 17. The third part, 'index++' increments the 'index' variable. It will increment once every time the loop completes and gets back to the beginning. As you can see, the second and third parts of the 'for loop' structure work together.

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 06:26 AM

Here is how a for loop works. There are the following components:
```for(<var-declaration>; <boolean-condition>; <var-modification>){
//code
}

```

The variable you declare and initialize is int i = 1;. The loop stops when the condition becomes false. So while i <= 17, the loop continues. You increment i by 1 on each iteration. So the way the loop evaluates is <var-declaration>, <boolean-condition>, the code in the loop body, then the <var-modification>. It then checks the condition again, repeating steps 2-4 until the condition becomes false.

### #4 Kilorn

• XNArchitect

Reputation: 1361
• Posts: 3,534
• Joined: 03-May 10

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 06:27 AM

For loops are most often used to increment the index of an array when you want to loop through each element and perform some logic for each element. Instead of writing the same statement x amount of times, you can just create a for loop and write the logic once.

I'll try to break down the parts of a for loop to loop through elements of an array:
```int x = 0; // Just sets a variable to 0, nothing special here.
x < arrayName.Count; // Tests to see if x is less than the number of elements in the array
x++ // Increments x by 1 so that it will step to the next element.

// Putting it together:
for (int x = 0; x < arrayName.Count; x++)
{
// Perform the logic here.
// This logic will be performed each time the conditional statement 'x < arrayName.Count' is true.
}

```

I hope this helps you understand it a little better.

EDIT: Wow, lots of answers at the same time.

This post has been edited by Kilorn: 21 September 2011 - 06:28 AM

### #5 Viske

Reputation: 24
• Posts: 70
• Joined: 07-June 11

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 06:27 AM

A for loop is
```for (initialization; termination; increment) {
statement(s)
}

```

Termination -index <= 17 - means that while index is less than or equal to (<=) 17, the loop will cycle. The increment is the amount to increment the count (index, in this case). The increment doesn't have to be index++, but it usually is.

The statement is what will execute each time you go through the loop (18 times in this case).

Edit: Others beat me to it.

This post has been edited by Viske: 21 September 2011 - 06:28 AM

### #6 creative code monkey

Reputation: 14
• Posts: 45
• Joined: 14-September 11

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 06:30 AM

Wow, I can't believe how fast people respond on this site. Good to know I found the place to be if you are a developer in need of help.

### #7 cmpshr

Reputation: 42
• Posts: 120
• Joined: 22-August 11

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 09:27 AM

Locke wrote a tutorial about the different loops

http://www.dreaminco.../59169-looping/

### #8 Okysho

Reputation: 4
• Posts: 96
• Joined: 09-February 11

## Re: For Loops (Explain - Beginner)

Posted 21 September 2011 - 02:19 PM

lmharmon, on 21 September 2011 - 06:20 AM, said:

So, I'm fairly new to Java and I'm having a bit of trouble understanding for loops. Say you want a code that first sets a variable total to 0 and then uses the for loop to add 3 to total 17 times. I know the code is this:

```int total = 0;
for (int index = 1; index <= 17; index++)
{
total = total + 3;
}

```

...But I don't quite understand why. I understand that int total = 0, but not the rest of it. ?

The others above me are pretty good, but here's how I sum things up:

you understand
``` int total = 0;
```
good. The rest is pretty similar. Let's break it down.

``` for (int index = 1;)
```
This is a variable declaration look closely.
``` int index = 1;
```
what's around it
``` for ()
```
is the structure for a for loop. [for] indicates that you want to begin a for loop. The brackets is just a special zone to give the conditions for the loop. Without these conditions, a for loop would not know how long to continue for.

```index <=17;
```
This is a simple boolean (true/false) statement. It always goes in this spot. It basically says this. "As long as [index] is less than or equal to [17]..."

``` index++
```
This is a function built in to most modern program languages. It simply means "The value of index, +1". There is also:
``` index--
```

Now lets look at the last bit.
``` {

total = total + 3;

}

```
The braces (these things "{") just provide a space for code within the loop. Everything you do only in this for loop, goes into these braces.

``` total = total +3
```
This is some simple addition and saving it to a variable. You're saying that the value of "total" is equal to the current value of "total" (which is "0") and you're adding 3 to it. This means that once this code is executed, the new value for total will be 3.

Need more explanation? I'll be happy to indulge.