# Thread: Tell me how this loop works.

1. Member Join Date
Jan 2010
Posts
15
Rep Power
0

## Tell me how this loop works.

So what i have in my head is this:
Step 1) arr1[i] = arr1[1-1] + 1
Step 2) arr1[i] = arr1 + 1
Step 3) ???

what is most troubling is that there is arr1 which i dont know how it existed from the above steps. Next is how arr1 is 0 when arr1 doesn't even exist. Shouldn't it have started out with arr1? Since i = 1?

Java Code:
```		for (i = 1;i <= max;i++)
arr1[i] = arr1[i-1] + i;;```
the whole code is this:
Java Code:
```System.out.print("Please enter the max number:");

int max = input.nextInt();

int[]arr1 = new int[max+1];
int[]arr2 = new int[max+1];
int[]arr3 = new int[max+1];

int i = 1;

// For-loop to calculate
for (i = 1;i <= max;i++)
arr1[i] = arr1[i-1] + i;

for (i = 1;i <= max;i++)
arr2[i] = arr2[i-1] + i;

for (i = 1;i <= max;i++)
arr3[i] = arr3[i-1] + i;

for (i = 0; i <= max; i++)
System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);

System.out.println("Sum of All is " + arr1[max]);```
what it prints out:
Java Code:
```Please enter the max number:5
Arr1 0 Arr2 0 Arr3 0
Arr1 1 Arr2 1 Arr3 1
Arr1 3 Arr2 3 Arr3 3
Arr1 6 Arr2 6 Arr3 6
Arr1 10 Arr2 10 Arr3 10
Arr1 15 Arr2 15 Arr3 15
Sum of All is 15```
Any help would be appreciated. Thanks!:)  Reply With Quote

2. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

## Java implicitly initializes all uninitialized int variables to 0, including elements of int[] arrays. It's sloppy practice to let the compiler do that, though. It's much better to initialize variables yourself, for the sake of clarity and readability. And arrays always start at index 0. If you create an int[max + 1] array, it will have max + 1 elements (obvious, isn't it?), ranging from 0 to max.

-Gary-  Reply With Quote

3. Member Join Date
Jan 2010
Posts
15
Rep Power
0

## Thanks.
But can you explain how the program runs it? Similar to how I wrote down the steps previously.  Reply With Quote

4. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

##  Originally Posted by hydride Thanks.
But can you explain how the program runs it? Similar to how I wrote down the steps previously.
I'll try, but the program as a whole doesn't make a lot of sense to me.
Java Code:
```System.out.print("Please enter the max number:");

int max = input.nextInt();```
You get an int from the user, and save it as max. In your sample run, it was 5.
Java Code:
```		int[]arr1 = new int[max+1];
int[]arr2 = new int[max+1];
int[]arr3 = new int[max+1];```
You create three int[] arrays, all of which go from arrX to arrX[max] (arrX in our sample run). The compiler implicitly initializes all array elements to 0.
Java Code:
`		int i = 1;`
This line is pretty much useless, except that it saves declaring i in each of the for loops below. The initialization to 1 is completely meaningless, because i is re-initialized in each for loop.
Java Code:
```		// For-loop to calculate
for (i = 1;i <= max;i++)
arr1[i] = arr1[i-1] + i;```
1st time through loop: arr1 = arr1 + 1 = 0 + 1 = 1
2nd time through loop: arr1 = arr1 + 2 = 1 + 2 = 3
3rd time through loop: arr1 = arr1 + 3 = 3 + 3 = 6
4th time through loop: arr1 = arr1 + 4 = 6 + 4 = 10
5th time through loop: arr1 = arr1 + 5 = 10 + 5 = 15
Java Code:
```
for (i = 1;i <= max;i++)
arr2[i] = arr2[i-1] + i;

for (i = 1;i <= max;i++)
arr3[i] = arr3[i-1] + i;```
You do the exact same thing with the other two arrays, for no reason I can guess, and of course you get the same results in those arrays.
Java Code:
```
for (i = 0; i <= max; i++)
System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);

System.out.println("Sum of All is " + arr1[max]);```
You print out the results, but "Sum of All" seems kind of meaningless -- what you're reporting is just the contents of the last element of the first array.

Hope that helps.

-Gary-  Reply With Quote

5. Member Join Date
Jan 2010
Posts
15
Rep Power
0

## awesome explanation!:D
But when you run it, why does it print out 0 first and then 1, 3, 6...?

I.E.
1st time through loop: arr1 = arr1 + 1 = 0 + 1 = 1
2nd time through loop: arr1 = arr1 + 2 = 1 + 2 = 3
3rd time through loop: arr1 = arr1 + 3 = 3 + 3 = 6
4th time through loop: arr1 = arr1 + 4 = 6 + 4 = 10
5th time through loop: arr1 = arr1 + 5 = 10 + 5 = 15  Reply With Quote

6. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

##  Originally Posted by hydride awesome explanation!:D
But when you run it, why does it print out 0 first and then 1, 3, 6...?

I.E.
1st time through loop: arr1 = arr1 + 1 = 0 + 1 = 1
2nd time through loop: arr1 = arr1 + 2 = 1 + 2 = 3
3rd time through loop: arr1 = arr1 + 3 = 3 + 3 = 6
4th time through loop: arr1 = arr1 + 4 = 6 + 4 = 10
5th time through loop: arr1 = arr1 + 5 = 10 + 5 = 15
Because your reporting loop starts at i = 0, not at i = 1.

-Gary-  Reply With Quote

7. Member Join Date
Jan 2010
Posts
15
Rep Power
0

## Ohhh. Wow, i did not see that. Thank you so much!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•