• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Loops: While, Do/While etc.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have simplified my question as much as possible to avoid confusion. I need a loop to iterate until a condition is met but when the condition is met I need for it to execute a different set of instructions. I understand this is very bad practce, but this works for me:



I can use a while or do/while loop, but then I cannot succesfully stop the loop to arrive at ==> Click End! and if I put 'if' statements inside the loop that seems repetitve and removes the whole point of the while or do/while loop. For example:








 
Marshal
Posts: 80230
424
  • 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 don't try to simplify things too much; there is a risk of losing the actual problem like that.
The problem you described is sentinel‑controlled iteration; that is usually implemented with a while loop. A do loop always executes at least once, so that might be less appropriate to your current problem.
I don't like while (true) ... because the infinite loop is the epitome of a program gone wrong and break; is outwith the conventions of structured programming. Yes, it does obviate the point of the loop.
You nearly stated the correct solution yourself:-

need a loop to iterate until a condition is met . . . execute a different set of instructions.

Correct. But you don't need the different instructions in the loop. You can simplify your code when you realise that lines 4 and 7 test exactly the same thing.
 
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 for the prompt response. I'm sorry, I still don't understand. Clearly the first example is very bad coding as I already stated hence the reason for my post. Sure if I wanted to simplify the second example I could (I could remove line 4 as you stated because it checks the same thing again), but to my understanding any if statement within the while loop is repetitive and probably poor coding also. I cannot see a way of doing it without though. If I do use if then rightly or wrongly I could use virtually any loop.
 
Bartender
Posts: 10979
87
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
Matches 2nd example.
-- OR --


 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternative, which is not semantically equivalent to Carey's examples:-I think I would use var instead of int. Remember var can only be applied to local variables.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although a while is probably the best sort of loop to use, it is not difficult to replace it by a for loop.
 
Carey Brown
Bartender
Posts: 10979
87
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

Campbell Ritchie wrote:Alternative, which is not semantically equivalent to Carey's examples:-I think I would use var instead of int. Remember var can only be applied to local variables.


That was my original inclination but it doesn't match the OP's logic because printing END is no longer dependent on the initial condition of current < target. My assumption is that line 3 is "//...".
 
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
Your assumption is mostly right. In fact it isn't line 3 //... where the code is, it is the



This is inputted from another program that I have no control over. All I do is check this variable and if it has incremented by 2 then I make my current increment by 2 as well. When you say OP's logic do you mean I am doing it the wrong way around? Is there another way?

To be honest, I'm having difficulty asking the question. I don't know exactly what to ask. My question perhaps isn't so much a coding question, if it was I would just Google it,  my question is like a coding logistics one. If I need to learn a better way I wish I knew what it was.

I would be happy if the while loop exited the loop immediately when the current was the same as target. Otherwise I just repeat myself with if statements.

Once again I am back to this. I can't find another way of doing it:

 
Carey Brown
Bartender
Posts: 10979
87
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
My examples were a more (I think) succinct way to express your logic which I don't believe Campbell's quite matches. Now, whether your logic matches the requirements I have no way to know. Both Campbell and I did take the approach of moving the logic of printing END outside of the loop which makes the loop itself easier to follow.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . your logic which I don't believe Campbell's quite matches. . . . .

You and I have interpreted the question differently, and as I said earlier, our two solutions are definitely not equivalent to each other. I shall leave it as an exercise for the reader to work out whether they can be made equivalent by swapping my lines 5 and 6. [They probably can't.]
* * * * * * * * * * * * * * *
What is that about another application incrementing the variable? That cannot be solved with a loop because the loop variables are local to the current method. You would have to take a different approach altogether.
Find out about Timers, maybe this sort, and maybe this sort. If in Swing®, consider a plain simple if‑else in the Action Listener to go with the Timer.
 
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
Yes, this is very close to what I mean:



Unfortunately, I cannot guarantee that current always increments (I failed to say that in my original post, sorry):



sb is from an external source. I make my 'local' integer called 'current' match that by incrementing it if sb is incremented. Unfortuntaely what still happens with this is the while loop always continues one to many times.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

w curawn wrote:. . .  I cannot guarantee that current always increments (I failed to say that in my original post, sorry) . . .

It is helpful to know the full details; it does no good to answer a question different frrom what you actually want answered. I did give you some hints last night.

. . . sb is from an external source. . . .

Not in that code it isn't. Do you mean that you are actually using somethingElse.getSb() in that loop? That is wasteful of resources, and it is probably not a good idea to use a loop at all. Least of all in Swing because you are either using multiple threads on a thread‑unsafe object, or occupying the whole of the EDT with a loop running 10⁸× a second for something occurring every few seconds. That can make your GUI unresponsive.
Also, never write == true and == false. Both are poor style and very error‑prone; it is only a matterr of time until you write = true by mistake, and all sorts of things can go wrong.
Never if (b == true) ... please.
Always if (b) ... please.
Never if (b == false) ... please.
Always if (!b) ... please.

. . . the while loop always continues one to many times.

How many times should it run and how many times does it run? But I now think a loop isn't going to work. Consider a timer, as I said last night. Consider some sort of Listener that calls a method whenever somethingElse.getSb() changes.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

w curawn wrote:Unfortuntaely what still happens with this is the while loop always continues one to many times.


What's your basis for coming to this conclusion? The number of times the loop will be executed cannot be predetermined so how do you know it was executed one too many times?
 
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
If I run the following example, which was also my original attempt (except I had “  ==> Click End!” Inside the loop):



I will get the output:

610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click again
618 ==> Click end

What I require is this output:

610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click end

Which is why I say the loop seems to iterate 1 too many times. But the only way I can find to avoid this is to have multiple ‘if’ statements within the loop which seems very repetitive and incorrect.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the while loop, try displaying first, then incrementing current. That is, move line 11 (the System.out.println statement) above line 9.
 
Carey Brown
Bartender
Posts: 10979
87
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
You say "current += 2" is a place holder, can you show us what the real code looks like?
 
Carey Brown
Bartender
Posts: 10979
87
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

 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

w curawn wrote:. . . the loop seems to iterate 1 too many times. . . .

Please explain some more.
You have a difference between the starting value and the finishing value of ten; if you increase the value by two, you will expect the loop to run five times.
 
Carey Brown
Bartender
Posts: 10979
87
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

w curawn wrote:I will get the output:

610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click again
618 ==> Click end

What I require is this output:

610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click end

OP is getting msg "again" and "end" printed back to back. What he wants is that when the end condition is met to ONLY print the "end" msg and not the "again" msg.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . move line 11 (the System.out.println statement) above line 9.

I tried that: didn't work.

Campbell's JShell wrote:jshell> int current = 608;
  ...>  int target = current + 10;
current ==> 608
target ==> 618

jshell> while (current < target) { <br />   ...>     System.out.println(current + " ==> Click again");
  ...>     current += 2;
  ...> } System.out.println(current + " ==> Click end");
608 ==> Click again
610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click end

Add 2 to current before the loop starts. But why do you need the loop at all, let alone running four times?
 
Carey Brown
Bartender
Posts: 10979
87
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

w curawn wrote:Your assumption is mostly right. In fact it isn't line 3 //... where the code is, it is the



This is inputted from another program that I have no control over. All I do is check this variable and if it has incremented by 2 then I make my current increment by 2 as well.

 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds simpler to use the distant variable directly, as I said earlier.
 
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

OP is getting msg "again" and "end" printed back to back. What he wants is that when the end condition is met to ONLY print the "end" msg and not the "again" msg.



That is exactly correct. It feels like it should be simple, but I just can't see it.

sb is extracted from another object within somebody elses code. On the site sb increments by 2 (but not every time, actually it seems to be every other time) up to a maximum of 10. The test should Click Again until the desired sb (or current = target) and then Click End.



Output is:
610  ==> Click Again!
612  ==> Click Again!
614  ==> Click Again!
616  ==> Click Again!
618  ==> Click Again!
618  ==> Click End!

But I want:
610  ==> Click Again!
612  ==> Click Again!
614  ==> Click Again!
616  ==> Click Again!
618  ==> Click End!

Please let me say, I really appreciate all the input and patience with this.

 
Carey Brown
Bartender
Posts: 10979
87
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
I gave you a solution twice and you haven't said why it won't work for you.
 
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

I gave you a solution twice and you haven't said why it won't work for you.



I stated earlier that it was very close to what I am trying to achieve, but I cannot guarantee that current always increments.



If I could insert the if statement into the while statement I think it would work. Whether or not it is acceptable is another matter. For example (I know it's not working):



Could this even work?
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

w curawn wrote:. . . Whether or not it is acceptable is another matter. . . . Could this even work?

You should by now now whether it will compile and run. If you insist on putting statements inside loop headers, you should use the ?: operator.
 
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

If you insist on putting statements inside loop headers, you should use the ?: operator.



I don't know any other way?
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haven't they taught you about ?: yet? It is something every beginner should know. It is also something every beginner seems to get confused about. You converttoand then you shorten it toYou will most probably have to enclose that expression and/or the assignment in () because of the low precedences of = and ?;. Note:-
  • 1: I can think of different versions that would be semantically equivalent to each other.
  • 2: Using = and using += may be semantically different if the operands have differrent types.
  •  
    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


    If you insist on putting statements inside loop headers…



    I meant I don’t know any other way of doing it without putting statements inside loop headers. But the comments are well received.

     
    Campbell Ritchie
    Marshal
    Posts: 80230
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Shot of changing target to 618, we haven't found another way to get your loop to stop earlier. Statements inside loop headers are an effective way of writing illegible code. I would however accept this sort of thing:-Note you need an additional pair of () here, too.
     
    Junilu Lacar
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    w curawn wrote:I will get the output:
    610 ==> Click again
    612 ==> Click again
    614 ==> Click again
    616 ==> Click again
    618 ==> Click again
    618 ==> Click end

    What I require is this output:

    610 ==> Click again
    612 ==> Click again
    614 ==> Click again
    616 ==> Click again
    618 ==> Click end



    Gives the desired output:

    610 ==> Click again
    612 ==> Click again
    614 ==> Click again
    616 ==> Click again
    618 ==> Click end

     
    Campbell Ritchie
    Marshal
    Posts: 80230
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Damn! Such a good bit of code. How did the rest of us miss it?
     
    Junilu Lacar
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This also produces the desired results:
     
    Junilu Lacar
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Damn! Such a good bit of code. How did the rest of us miss it?


    Well, you didn't, which is why I was puzzled by your previous reply:

    Campbell Ritchie wrote:

    Junilu Lacar wrote:. . . move line 11 (the System.out.println statement) above line 9.

    I tried that: didn't work.
    ...
    Add 2 to current before the loop starts.

     
    Campbell Ritchie
    Marshal
    Posts: 80230
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You mean I missed that I didn't miss it. I think I need more beer.
     
    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
    I know that line 5 and 11 are repetitve but at my limited stage of experience, this is the best I can come up with:

     
    Sheriff
    Posts: 8988
    652
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    w curawn wrote:this is the best I can come up with


    People are giving various suggestions how to approach this problem, but, given a nonsensical (sorry for this word, no intention to be harsh) provided example what need to solve, it is quite difficult to understand whether given suggestions may satisfy your need.
     
    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


    People are giving various suggestions how to approach this problem, but, given a nonsensical (sorry for this word, no intention to be harsh) provided example what need to solve, it is quite difficult to understand whether given suggestions may satisfy your need.



    I am aware I have not explained it well, I certainly don’t claim to be able to use the correct terminology and if knew how to precisely ask the question obviously I would. If the problem was simply a coding one, I would just Google it, but as I stated before it seems more like a logistical one as though I am making a simple mistake like not putting something in the correct order.

    This is very close, but current doesn’t always increment only if test is true, so I don’t know how to implement this:



    I would love to ‘get it right’ and learn the correct way I am to do it, but I don’t know what exactly I am doing wrong. Please I would greatly appreciate patience on this. Thank you
     
    The overall mission is to change the world. When you've done that, then you can read this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic