• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

The flow control: for

 
Greenhorn
Posts: 21
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you post the corrected code, let us know which part you cannot understand
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lorenzo Tagliaferro
Greenhorn
Posts: 21
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys...
This is the corret code!!
I have to understand how this working!

Thanks again!

 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ;-)
 
Lorenzo Tagliaferro
Greenhorn
Posts: 21
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys!!

Your tips are very important for me!!

I'm studying to become like you...a Java Programmer Certified!! ;-)







 
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
reply
    Bookmark Topic Watch Topic
  • New Topic