1. Member Join Date
Dec 2008
Posts
4
Rep Power
0

## Basic help here please....for each loop

Hallo All;

I have a question please regarding For-Each style for loop, just puzzeled i'm new to java and i'm reading a very wonderful book "Java a Beginner's guide" by Herbert Schildt. here goes the code and my question:

Java Code:
```class ForEach2{
public static void main(String args[]){
int sum = 0;
int nums[] [] = new int  ;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);

for(int x[] : nums) {
for(int y : x)  {
System.out.println("value is : " + y);
sum += y;
}
}
System.out.println("Summation: " +sum);
}
}```
the out put from this program is

value is: 1
value is: 2
value is: 3
value is: 4
value is: 5
value is: 2
value is: 4
value is: 6
value is: 8
value is: 10
value is: 3
value is: 6
value is: 9
value is: 12
value is: 15

Summation: 90

we have here four 'for' loops, i am just lost. can any way explain to me please. what value does exactly has 'y' here? i know for some of you it is very easy but i cann't get it.  Reply With Quote

2. ## Pick things apart a little bit and play with it to see what's going on.
Like this:
Java Code:
```public class Test {
public static void main(String[] args) {
int sum = 0;
int[][] nums = new int  ;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);

print(nums);

for(int[] x : nums) {
print(x);
for(int y : x)  {
System.out.println("value is : " + y);
sum += y;
}
}
System.out.println("Summation: " +sum);
}

private static void print(int[][] n) {
for(int i = 0; i < n.length; i++) {
System.out.printf("num[%d] = ", i);
print(n[i]);
}
}

private static void print(int[] n) {
System.out.print("[");
for(int i = 0; i < n.length; i++) {
System.out.print(n[i]);
if(i < n.length-1) {
System.out.print(", ");
} else {
System.out.print("]\n");
}
}
}
}```  Reply With Quote

3. Member Join Date
Dec 2008
Posts
55
Rep Power
0

## i'm confused about that syntax... newbie here too. when the for loop says for (int x[] : nums) does that mean its going through each value of the two dimensional array? i've never yet encountered that syntax of int x[].  Reply With Quote

4. Member Join Date
Jan 2008
Posts
31
Rep Power
0

## what is this for(int x[] : nums)
& what is this for(int y : x)
what does the ':' do?

and you can read more here h t t p : / /
java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

I think what I wrote below is a waste of time

Because we have 15 "value is: "
I take it, it is only using the i++ and J++
value is: 1 // i = 0 j = 0
value is: 2 // i = 0 j = 1
value is: 3 // i = 0 j = 2
value is: 4 // i = 0 j = 3
value is: 5 // i = 0 j = 4
value is: 2 // i = 1 j = 0
value is: 4 // i = 1 j = 1
value is: 6 // i = 1 j = 2
value is: 8 // i = 1 j = 3
value is: 10 // i = 1 j = 4
value is: 3 // i = 2 j = 0
value is: 6 // i = 2 j = 1
value is: 9 // i = 2 j = 2
value is: 12 // i = 2 j = 3
value is: 15 // i = 2 j = 4
Last edited by tghn2b; 12-30-2008 at 09:41 AM.  Reply With Quote

5. ## Syntax of the enhanced for loop, aka, the for-each loop. For more about this see The ForEach Loop.  Reply With Quote

6. Member Join Date
Jan 2008
Posts
31
Rep Power
0

## OK it seems to be also called "for-in loop."

so there is not
we have here four 'for' loops,
There is two "for loops" and two "for-in loops."

bugger://leepoint.net/notes-java/flow/loops/foreach.html
Last edited by tghn2b; 12-28-2008 at 08:28 AM. Reason: in red (I was making my reply when "hardwired" posted)  Reply With Quote

7. Member Join Date
Dec 2008
Posts
4
Rep Power
0

## Ohh thanks All!

hardwired let me try ur way and let me read the links i'll be back with result
hopefully will get it all.

thanks again  Reply With Quote

8. Member Join Date
Dec 2008
Posts
4
Rep Power
0

## Hi again

so far so good,

Java Code:
`print(nums);`
prints these three arrays, am i right?

num = [1, 2, 3, 4, 5]
num = [2, 4, 6, 8, 10]
num = [3, 6, 9, 12, 15]

and the
Java Code:
`print(x)`
does this
[1, 2, 3, 4, 5]

and
Java Code:
`sum += y;`
does this

[2, 4, 6, 8, 10]

and last what i got is
Java Code:
`for(int y : x)`
the value of x is in y right?

thanks again for the links and info.
i will be back with more puzzels, just want to understand how java is hopefully i'm in a good hand here :)  Reply With Quote

9. ## don't go buggy at Whip and Buggy Software Company

for(int y : x) would be read, for each int in x, which to answer tghn2b and diggitydog, the elements may in fact be ints, arrays, could even be SomeObsucreClassWroteToBeABigObscureClass. or an array of them. If the elements of an array are arrays, you may use the variable[index] code to "talk" to each element in the sub-array.

We see some bizzare looking sytax in Artificial Intelligence that looks like:
Java Code:
`variable = first[index_1][index_2].getValue();`
Or one which I use all the time:
Java Code:
`System.out.println(array[index].toString());`
which I use for de-bugging, so called because you go buggy trying to use de-buggers.

de buggers are full of bugs, an ......

oh, well. sorta gets re-cursive.....
Last edited by Nicholas Jordan; 12-30-2008 at 04:52 PM.  Reply With Quote

10. Member Join Date
Dec 2008
Posts
4
Rep Power
0

## thx Nicholas Jordan  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
•