• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Loops: While, Do/While etc.

 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

w curawn wrote:. . . . If the problem was simply a coding one, I would just Google it . . . .

Careful; there is no guarantee that a web search will yield good results, and we often see poor code or out of date practices from web searches.
 
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-- OR --

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole purpose of the while loop is to check whether the returned integer fetchSb has incremented or not and keep doing so until the increment has totaled 10. current is 'my' integer, which is why I can't just increment current by 2 every iteration..
 
w curawn
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the patience and help... it is much appreciated.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

w curawn wrote:Thank you all . . . .

That's a pleasure
 
Greenhorn
Posts: 27
1
IBM DB2 Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Might be beating a dead horse, as this seems to be similar to some of the code above.
Eliminated a couple of lines, at the expense of readability using ternary statements.
Try uncommenting line 7 as it will print out duplicate "click again" statements for each (really I just wanted to see what happens when you turn off increment flag).

Ahh,  a day late and a dollar short. LOL
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not bad. The reason for the poor legibility is that you didn't indent your ?: expression correctly and therefore got your lines too long. Try the following:-Did you notice the repetition? You can remove it like this:-or
 
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Guys,

I have tried as below and it seems to me that it would match the requirement of the orginal poster. Your thoughts please:
Moreover, this is my first ever post/reply/comment in coderanch.

 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you have tested your program. This is what I got from JShell:-

TestCodeRaunch.main();
608
 ==> Click Again
608
 ==> Click Again
610
 ==> Click Again
610
 ==> Click Again
612
 ==> Click Again
612
 ==> Click Again
614
 ==> Click Again
616
 ==> Click Again
618  ==> Click End

Is that what is supposed to happen?

I corrected your code tags; the [/code] goes at the end of the code block and formatting tags cannot be used inside code tags. Doesn't it look better now But your long comments should be /* comments */. Otherwise the lines are too long to read.
 
Jawahar Raj N
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for the welcome and also for formatting my code and the feedback on comments  
Yes, I have run the program myself a few times before posting it here.
There were 2 points from Mr.Curawn's requirement that must be honoured in the proposed solution.
       
            1. The whole purpose of the while loop is to check whether the returned integer fetchSb has incremented or not and keep doing so until the increment has totaled 10. So, current can't just be incremented by 2 in each iteration.
           2. When fetchsb (or the latest current value post increment) exatctly matches the target, then different set of statements should be executed i.e. printing "Click end" in this example.

While most of the answers tried to address point 2, I feel the point 1 is also crucial and can't be ignored. From the point 1, we can say the ultimate goal is not to solve the problem by using a loop (while or other loops) but instead while loop was used as just a means of solving it.

Hence, I feel, we dont need a loop here, because as we know, a loop will always evaluate the conditon only "after" each iteration and not automatically break/exit in the middle of one such iteration just because some operation resulted in reversing the condtion's trueness.

I think, this was the crux of the dilemma in Mr. Curawn's question, where in he was using an if (to evaluate the same loop conditon) within the loop.

As per point 1, as the variable is not always incremented and to simulate that uncertainty, I have used nextBoolean() of Random class in my solution. So, on different runs, we might see a slightly different output wherein the retry is suggested a few additional times at certain values.But, as in point 2, it exits and prints a different msg when the variable exactly matches the target value.

My solution could very well be wrong if my interpretation of the problem itself is incorrect. Would be better if Mr. Curwan gets to check and confirm this.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I had forgotten about your only incrementing the loop variant at random, so your solution does appear to be correct.
Remember that a loop is semantically equivalent to a recursive program.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please beware of editing posts; use the preview button to see what you have written before submitting thee post.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
   ---------------------------  OR---------------------------------
 
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope you don't recommend mixing += and <
Does that fulfil the requirements? Maybe you should print 608 etc. first.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public static void main(String[] args) {

int current=608;

int target = current +10;

while(current<target) {
System.out.println(current +"===>Click Again");
current+=2;
}
System.out.println(current +"===>Click End");

}
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that will work correctly, and welcome to the Ranch
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic