The flow control: for
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have problem tu understand the iteration of the flow control.
Someone can help me to understand the iteration of this code?
Thank in advance!
Someone can help me to understand the iteration of this code?
Thank in advance!
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Actually the above code will not compile because the variable j is declared in the inner loop and you are trying to use it in the outer loop. The compiler will say j cannot be resolved
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Once you post the corrected code, let us know which part you cannot understand
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As the others have said, line 7 contains an error, because the variable j is out of scope at that point - that is, the variable j doesn't exist anymore, it only exists in lines 4-6. Suppose that we comment out line 7.
The for keywords creates a loop. In your example, you have a loop over variable i going from 0 until and including 9. Inside that loop, there is a nested loop over variable j. Note that the condition for the nested loop is j<0. That means the loop will never iterate, because j is never less than 0.
See Control Flow Statements in Sun's Java tutorials to learn about the for statement and other control flow keywords of Java.
The for keywords creates a loop. In your example, you have a loop over variable i going from 0 until and including 9. Inside that loop, there is a nested loop over variable j. Note that the condition for the nested loop is j<0. That means the loop will never iterate, because j is never less than 0.
See Control Flow Statements in Sun's Java tutorials to learn about the for statement and other control flow keywords of Java.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Sorry guys...
This is the corret code!!
I have to understand how this working!
Thanks again!
This is the corret code!!
I have to understand how this working!
Thanks again!
posted 15 years ago
Hi
Lorenzo
first the outer loop will run once for value of i and the inner will run thrice for value of j
the again the loop will increment the value of i to 1 and the inner will run thrice for value of j
this will continue until the value of i is reache to 3 and loop will exit.
output is 147258369
hope this helps.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a1[j][i]);
}
}
Hi
Lorenzo
first the outer loop will run once for value of i and the inner will run thrice for value of j
the again the loop will increment the value of i to 1 and the inner will run thrice for value of j
this will continue until the value of i is reache to 3 and loop will exit.
output is 147258369
hope this helps.
Thanks & Regards
Sumit Kothalikar
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Lorenzo,
for this type of questions, I generally recommend to write down the whole iteration steps on a piece of paper, for example like this:
i----------j----------a1[j][i]
0-----------0---------------1
0-----------1---------------4
0-----------2---------------7
1-----------0---------------2
1-----------1---------------5
1-----------2---------------8
2-----------0---------------3
2-----------1---------------6
2-----------2---------------9
The array a1 can be read like this:
a1[0] = {1, 2, 3};
a1[1] = {4, 5, 6};
a1[2] = {7, 8, 9};
Let's take the case where i=1 and j=2:
a[j][i] -> a[2][1] = 8
Hope this helps!
I advise to do like this even in the real exam, because the only fact of writing down can prevent you from the awkward oversights.
The devil is in the details ;-)
for this type of questions, I generally recommend to write down the whole iteration steps on a piece of paper, for example like this:
i----------j----------a1[j][i]
0-----------0---------------1
0-----------1---------------4
0-----------2---------------7
1-----------0---------------2
1-----------1---------------5
1-----------2---------------8
2-----------0---------------3
2-----------1---------------6
2-----------2---------------9
The array a1 can be read like this:
a1[0] = {1, 2, 3};
a1[1] = {4, 5, 6};
a1[2] = {7, 8, 9};
Let's take the case where i=1 and j=2:
a[j][i] -> a[2][1] = 8
Hope this helps!
I advise to do like this even in the real exam, because the only fact of writing down can prevent you from the awkward oversights.
The devil is in the details ;-)
SCJP 6 (88%), SCWCD (89%)
| If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









