Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What's the best way to work through nested loop questions?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I'm currently learning to pass the OCPJP11. One observation that I've made is that I spend way too much time on nested loop questions, and I find it's because I get confused easily trying to remember so much for each iteration. Is there a proven process of tackling these sort of questions, without spending 10 minutes trying to figure out if x was 7 or still 6 lol?
 
Marshal
Posts: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The good news is, many loops have become redundant since the introduction of Java8 nearly eight years ago. The bad news is, you still have write some loops and you still have to know loops to pass cert exam questions. I recommend you write lots of nested loop applications. Hundreds of them. Until you can look at a loop and see what it will do without any effort.
Start with those annoying programs which write shapes out of asterisks like the following:-Write many examples like that with nested loops until you think you start dreaming about nested loops
 
Kei Ichi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response. Perhaps I wasn't clear enough, is there a certain number of steps people take when analyzing and resolving nested loop based questions, specifically ones that use branching statements such as break, continue?

Campbell Ritchie wrote:Welcome to the Ranch

The good news is, many loops have become redundant since the introduction of Java8 nearly eight years ago. The bad news is, you still have write some loops and you still have to know loops to pass cert exam questions. I recommend you write lots of nested loop applications. Hundreds of them. Until you can look at a loop and see what it will do without any effort.
Start with those annoying programs which write shapes out of asterisks like the following:-Write many examples like that with nested loops until you think you start dreaming about nested loops

 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These kind of suck, and some (many) people will tell you that you should really never, or almost never, use labelled break or continue in your code, for the same reason we hate to see it on a test with the time tick-tick-ticking away, it takes a long time to read such code correctly.

So, even tho what I am going to say seems obvious, and even tho I still find these to take at least twice as long as almost any other question on the exam (and I have been writing nested loops for about 40 years now!)...



What I would remind you to know here is that continue inner and continue would be the same, so on a fair number of questions where they ask you "which ones will output <Some Output>?" either both those answers must be true or both of them must be false, right?

I don't encourage anyone to cheat, but it is possible that there are much better uses of your time in learning Java than in getting super-fast at these slowest of all questions on an exam that punishes spending too much time on any question very harshly.

So, you might see some where "I know if A is true C must be" -- this can either shave time off your correct answer fully worked out at amazing speed, or allow you to save time by guessing something logically sound saving those extra seconds (minutes?) for other questions.

I just realized I might be projecting a bit, as for me I could get 100 of them right in a row, they just take too long and I am pressed for time, but, the standard advice for most people is:

use your scratch area "pad of paper" or whatever for your exam to keep track of the variables which inevitably are changing thru each part of the loop.

i        j        prints
 0        0            nothing!
 0        1                        1
 0        2                        2

If you are doing this write big enough to write very fast and still be readable to you as you go along and small enough to not run out of room...

There may be other things you can do to get at least a probable answer very quickly, by looking quickly at the answer choices and back to the loops.  I find I can sometimes eliminate an answer or two that way, and on the real exam if they tell you PICK 3 CHOICES that might get you over the finish line sometimes.

In Real Life, where lots of people write interesting code you might find yourself looking at that doesn't follow best practices all around, this skill is actually sometimes pretty useful -- I have definitely seen code that was doing interesting enough stuff I wanted to understand it, modify it or debug it, that DID use these features, and it really isn't hard to take pen or pencil and paper and figure it out.

It is *not* so easy to look at it for 5 seconds and see everything, I think.

The programs Campbell recommended writing can be a lot of fun and sort of interesting, but there is also a lot to cover in the stuff you are studying...I can usually write those pretty well, not quite the same skill as matching nested loops with labelled break and continues to statements about their output.

It probably wouldn't hurt, but consider the possibility that after you study the heck out of everything, you may look back on these and say "If I skip ANY question to come back to it will definitely be one of these time sucks!" -- there are many harder things covered on the exam, for sure, but very few that take more time to answer with confidence.

 
author & internet detective
Posts: 41086
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I second (or third) "write stuff down". Also remember to check for compiler errors first so you don't spend time tracking variables for no reason.
 
Die Fledermaus does not fear such a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic