• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Need tip/advise to solve complex looping

 
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need your advise, any simple tip to solve complex looping in quick time. Some examples:

Sample 1://Looks simple I found it hard to calculate end result, only missed by one iteration few: answer was wrong



Sample 2: //would be simple if choice were easy
1>It will print x, y when line 1 is replaced by break;
2>It will print x, y when line 1 is replaced by continue;
3>It will print a, b when line 1 is replaced by continue;
4>It will print a, a when line 1 is replaced by i = m++;
5>It will print b, b when line 1 is replaced by i = 4;

Just modified the original choices, just to make my point without violating any contract. Here x,y,a,b are numbers ranging from 1,2,3,4. I could spot 1st 3 choices within time but last 2 were hard to calculate, as I had only few seconds to solve this.

Until now I practiced this, to solve
Sample 1: c=0, s=0, c<11
c=1, s=1, c<11
c=0,3,6,9,12 sum wont change
like wise on paper where c=count and s=sum, but this led me to the confusion, will the loop enters, when c=10 as its count++ not ++count.

Any simple steps/tips that you followed, and worked for you, would be great.


Thanks,
With kind regards,
Prathima
 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no simple rule. You just have to think like the compiler! That being said, you can always as a first step look for errors in the given code snippet.
 
Ranch Hand
Posts: 472
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. on exam use paper and write each iteration.
2. before you start writing check for compiler error.
3. before 1 check condition and write last that be before you exit loop.
ps. question like this claim maximum attention and time (my own opinion)


you use post increment so last condition is 11
1 2 3 4 5 6 7 8 9 10 11
1+2+4+5+7+8+10+11=48
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow!!! @Sergej Smoljanov, I really liked your reply. Thanks.

Sergej Smoljanov wrote:
ps. question like this claim maximum attention and time (my own opinion)



I completely agree with you.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prathima gaitonde wrote:I need your advise, any simple tip to solve complex looping in quick time.


Unfortunately there is no simple rule or algorithm to solve this kind of complex loop questions. But there are a few tips you can apply with such a question (or even with every question):
  • before you start to evaluate a code snippet, always glance quickly at the possible answers. If there's no "compilation fails" answer, don't waste your precious time checking if the code will compile or not. If there's a "compilation fails", you should 1st check if the code snippet compiles before you start evaluating the code itself (it would be a real waste of time if you have spent 5 minutes evaluating a really complex loop when you discover the print statement uses a variable outside its scope). Sometimes you might see possible answers like "Compilation fails due to an error on line X"; then you should only check these lines carefully for compiler errors
  • the exam center is required to give you some scratch paper and a pen. If they don't give it to you, just ask (you are entitled to get this stuff). These days it's often an erasable small whiteboard (or an alternative), so you get a wiper and a special pen as well. Use these aids to work out complex loops by writing down the state of every (important) variable at every iteration. Even if you are an experienced developer (like me), you solve these kind of questions like this.
  • and finally: as always, practice makes perfect. The more you solve these kinds of questions, the better and faster you'll be able to solve them


  • So if we apply the 2nd bullet to sample1 (the one with count & sum variables), your whiteboard/paper would look something like this:


    Hope it helps!
    Kind regards,
    Roel
     
    Sergej Smoljanov
    Ranch Hand
    Posts: 472
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    so last condition is 11


    Roel right last count will be 12 (but outside loop and after checking condition)

    when rechecking (my own answer) i make mistake. so if you rechecking - use paper too.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sergej Smoljanov wrote:you use post increment so last condition is 11
    1 2 3 4 5 6 7 8 9 10 11
    1+2+4+5+7+8+10+11=48


    I have to nitpick a little bit, as this isn't 100% correct. The iteration with count = 0 is missing, although this won't affect the result in the end (0 + 0 = 0 ). And the final value of count will be 12 (also not important for the result, but could be if count was printed as well).

    If you had a few small print statements to the code, you can actually see what's going on. This code snippet

    produces the following output:
    c: 0 c: 1 -> s: 1
    c: 2 -> s: 3
    c: 3 c: 4 -> s: 7
    c: 5 -> s: 12
    c: 6 c: 7 -> s: 19
    c: 8 -> s: 27
    c: 9 c: 10 -> s: 37
    c: 11 -> s: 48
    The end! c: 12 s: 48


    Hope it helps!
    Kind regards,
    Roel

     
    Sergej Smoljanov
    Ranch Hand
    Posts: 472
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you are right. First i think that make mistake in checking last expression, but now see that i overlook first checking and this also important for exam. for example this one

    can be overlooked also.
     
    Ranch Hand
    Posts: 94
    3
    Eclipse IDE Oracle AngularJS C++ Chrome Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:

    Sergej Smoljanov wrote:you use post increment so last condition is 11
    1 2 3 4 5 6 7 8 9 10 11
    1+2+4+5+7+8+10+11=48


    I have to nitpick a little bit, as this isn't 100% correct. The iteration with count = 0 is missing, although this won't affect the result in the end (0 + 0 = 0 ). And the final value of count will be 12 (also not important for the result, but could be if count was printed as well).

    If you had a few small print statements to the code, you can actually see what's going on. This code snippet

    produces the following output:
    c: 0 c: 1 -> s: 1
    c: 2 -> s: 3
    c: 3 c: 4 -> s: 7
    c: 5 -> s: 12
    c: 6 c: 7 -> s: 19
    c: 8 -> s: 27
    c: 9 c: 10 -> s: 37
    c: 11 -> s: 48
    The end! c: 12 s: 48


    Hope it helps!
    Kind regards,
    Roel



    Hello

    Clarify something for me please.

    What exactly is happening here?


    As I stated in my very first post upon receiving my Associate Certification (OCAJP 7/8) book, I have knowledge in how Java operates but I am very far from expert. I hope I don't get any "you should already know that if you are studying from this book"
    Count starts at 0, so 0 divided by 3 is 0 which satisfies the reminder of == 0. What does this continue mean? If the statement evaluates to true then we do not add sum + count?

    I see, for instance, that when count is 3 the statement evaluates to true as well, so the continue again means that the code ignores the rest of the if statement and skips to to while loop?

    I hope my question was clear.

    Thanks!
     
    Daniel Andres
    Ranch Hand
    Posts: 94
    3
    Eclipse IDE Oracle AngularJS C++ Chrome Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sergej Smoljanov wrote:you are right. First i think that make mistake in checking last expression, but now see that i overlook first checking and this also important for exam. for example this one

    can be overlooked also.



    What is the answer here

    The end! c: 10 s: 10 ?
     
    Daniel Andres
    Ranch Hand
    Posts: 94
    3
    Eclipse IDE Oracle AngularJS C++ Chrome Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What I meant to say in my second to last post is that 3 divided by 3 is 1 which gives no reminder therefore it satisfies the == 0.
     
    Ranch Hand
    Posts: 789
    Python C++ Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Andres wrote:

    Sergej Smoljanov wrote:you are right. First i think that make mistake in checking last expression, but now see that i overlook first checking and this also important for exam. for example this one

    can be overlooked also.



    What is the answer here

    The end! c: 10 s: 10 ?


    It's a trick question...
    first thing, count increments to 11, so the loop never executes, and so it prints out...

    Your understanding of continue and % is correct. When you hit continue you abort what comes after for that current iteration and go back to the top of the loop.

    To figure these kinds of things with counts in them out, do like Roel suggests and make a list of what each variable becomes. You can check whether you were right or not by pasting the code and running it.

    The real test is full of this kind of stuff. Some you may not have to work out, like this one, but others you will. Get good at it.

     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Andres wrote:Count starts at 0, so 0 divided by 3 is 0 which satisfies the reminder of == 0. What does this continue mean? If the statement evaluates to true then we do not add sum + count?

    I see, for instance, that when count is 3 the statement evaluates to true as well, so the continue again means that the code ignores the rest of the if statement and skips to to while loop?


    You are spot-on! That's exactly how continue works. More info about break, continue and return can be found here.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Andres wrote:What is the answer here

    The end! c: 10 s: 10 ?


    No! And you are not even close

    But I'm pretty sure you'll get the correct answer if you work through the code and keep track of the different values for count and sum as I demonstrated in this post. It's an easy technique which will be very useful and helpful on the (actual) exam.
     
    The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
      Bookmark Topic Watch Topic
    • New Topic