• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Is there any "quick" way to resolve a nested loop?

 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Source: OUKC mock exams.
Hi Pals...Can any one advise me on the "quickest way" (if any) to resolve nested loops??, I burnt out toooo much time on them in my last two exams. I will appreciate your replies thanks.
Example:





A. 30 31 20 21
B. 30 31 32 20 21 22
C. 30 31 32 33 20 21 22 23
D. 30 31 32 20 21 22 10 11 12
E. 30 31 32 33 20 21 22 23 10 11 12 13
F. Compilation fails.

The answer is A. But like I said is there any "Short-Cut" to resolving this??.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't really a short-cut. What I'd recommend is drawing a quick table. In this case you'd have three columns - j, k and output. Then just step through the program, writing out the values as they change. It really shouldn't take that long - took me about a minute when I tried it just now (a little longer to format it below, though!), and the exam gives you quite a bit more than that per question.

So you'd get something like this:
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:There isn't really a short-cut. What I'd recommend is drawing a quick table. In this case you'd have three columns - j, k and output. Then just step through the program, writing out the values as they change. It really shouldn't take that long - took me about a minute when I tried it just now (a little longer to format it below, though!), and the exam gives you quite a bit more than that per question.

So you'd get something like this:



Thank you very much Matthew, I am going to start practicing with this.

 
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

Matthew Brown wrote:There isn't really a short-cut. What I'd recommend is drawing a quick table. In this case you'd have three columns - j, k and output. Then just step through the program, writing out the values as they change. It really shouldn't take that long - took me about a minute when I tried it just now (a little longer to format it below, though!), and the exam gives you quite a bit more than that per question.

So you'd get something like this:



This approach is pretty interesting. I usually maintain the value of the variables alone on paper for each run. At work this is never necessary since multiple nested loops and named blocks are rare.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:

Matthew Brown wrote:There isn't really a short-cut. What I'd recommend is drawing a quick table. In this case you'd have three columns - j, k and output. Then just step through the program, writing out the values as they change. It really shouldn't take that long - took me about a minute when I tried it just now (a little longer to format it below, though!), and the exam gives you quite a bit more than that per question.

So you'd get something like this:



This approach is pretty interesting. I usually maintain the value of the variables alone on paper for each run. At work this is never necessary since multiple nested loops and named blocks are rare.


@Deepak, I have been practicing this approach all day and it is REALLY interesting, like you said "At work" scenarios like these are RARE, but the SCJP exam organisers believe in "Good-Things-Dont-Come-Easy". Its left for us to take it OR leave it.

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quick noobie question what does "free:" do?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Joseph wrote:quick noobie question what does "free:" do?




"free:" is a label. In this example, it is used to label the outer loop. The "break free" statement is used to break out of the labelled loop -- breaking out of two loops at the same time.

Henry
 
Christian Joseph
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Henry appreciate the reply

 
Christian Joseph
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw my answer is letter A

Btw what does the label do mr henry?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Joseph wrote:
Btw what does the label do mr henry?



Didn't my last response answer this question? If not, can you elaborate more on this question?

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic