programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Language Fundamentals

Sri Sridhar
Greenhorn
Posts: 2

how many lines of output will be produced?

Can anybody explain the flow in detail...

Sri

[ October 26, 2004: Message edited by: Barry Gaunt ]

Barry Gaunt
Ranch Hand
Posts: 7729
Yes 11 lines is correct. 11 = 4 + 2 + 5, does that give you a clue?

rengarajan vaikuntam
Ranch Hand
Posts: 37
Sri
Its like this
Look at the condition of the first for loop
i<x.length
x.length is the length of the x array which is clearly declared in the statement
int [][][] x=new int[3][][];
so x.length is 3, ok, so the first for loop will run for 3 times for values of i=0, i=1, i=2 ok.
Now the next for loop
for(j=0;j<x[i].length;j++)
here the condition is j shoulbe lessthan x[i].length,so
when i is zero, the condition is j<x[0].length,ok
Look at the line 5 from ur code it says
x[0]=new int [4][];
so x[0].length is 4, so the condition is j<4;
so the inner for loop will run for 4 times for j values of j=0, j=1, j=2 & j=3.now the value of i is zero.Now the sout will print 4 times ok.
Now when i=1,
the condition become j<x[1].length ie j<2 now the inner loop will run for 2 times for j values of j=0 and j=1.Now the sout will print 2 times.
Now i=2,
condition j<x[2].length ie j<5 so now the inner j loop will run for 5 times.now the sout will print 5 times.
So totally
when i=0; it prints 4 times
when i=1; it prints 2 times
when i=2; it prints 5 times
So the answer is (5+2+4) 11 times.
Hope this helps you.

Sri Sridhar
Greenhorn
Posts: 2
Thanks Rangarajan that was a good explanation i got cleared now. Ofcourse i could find the clue earlier but this detailed flow cleared my doubts..thanks barry.