• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want to know about more advanced loops.  RSS feed

 
Oliver Sokol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out how to make a bit more advanced loops. I'm looking to have a piece of coding that will continue to check for a value, when it receives that value it enacts an if statement but continues to search for the value without stopping.

I would also like a loop that outputs a number continuously. The number will differ depending on input into the loop but it should continue to produce numbers over time.

Most probably a timer for execution of both loops will be what I will want to use so that testing for the value occurs every 1 second or ouput occurs every 1 second.

Is there any place where i can find guides that might help me with this, or can someone tell me what these loops would be called so that I can search them out myself?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say check for a value do you mean a keypress? What you do would depend on where the 'value' comes from. With a keyboard what you describe would be pretty trivial, but depending on where 'value' comes from it could need multithreading.
 
Oliver Sokol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for the reply.

Well I would want it to be a monitoring thing, so for example to observe a watch, and every time it sees that it is a specific time it will execute an if statement. Essentially it is not yet important where the input comes from more interested in learning how to make a loop that will continuously and repeatedly look for a value and continue even after it finds it.

Again thanks for replying.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver Sokol wrote:Hey, thanks for the reply.

Well I would want it to be a monitoring thing, so for example to observe a watch, and every time it sees that it is a specific time it will execute an if statement. Essentially it is not yet important where the input comes from more interested in learning how to make a loop that will continuously and repeatedly look for a value and continue even after it finds it.

Again thanks for replying.


Since it isn't important where it comes from, let's assume the handling of the value takes no time to do.... You could use something more or less like this:


In reality, everything depends on how fast the input comes, how much time what you have to do with the input takes, even whether you're allowed to miss some inputs. For starters look up the idea of a buffer. Some part of the system would put new input in the buffer and when you went back to the loop you would check the buffer to see if anything got put in there while you were off playing with the last input. Keyboard input is usually already buffered for example, so that's why I mentioned keyboard before. Rarely you might need two threads, with one reading input and buffering it and the other handling the buffered input. So you see, it really does depend on what you want to do.
 
Oliver Sokol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply Guillermo Ishi, it is very much appreciated. Thanks for taking the time.

Okay looks good, but does the while loop not stop once it reaches a certain determined value?

I was thinking of while command but thought that there was always a limit as to how long it would continue to check.

Also would there be a command for checking for example every other second instead of constantly? so that there is a countdown of 1 second before it checks?

I might need the two threads like you mention they seem to offer the direction in which I want to go.

After considering the idea for this little project further I've determined that this piece of code should really be just a messenger taking a specific input turning it into a threshold true or false that gets passed on. Thats why I want to know if there are timers for input checking and output setting. So for example if I was to automate input by making a random number generator from 0-1 that updated every 1 second. Then have the input checker test every 1 second as well for a change in the random generated number but only become true when that number is 1?

So the plan looks like this:
random number generator 0-1 updated every second.
Checking every second whether the number is 1
if number is 1 send message as true
if number is 0-0.9 send message as false.

That would be the initial prototype, I will make it more complicated with time, but I need to know where to start and whether there are timers like this. If you know of any sites with guides on how to make such timers or that would otherwise show me how to make such "checking" loops then it would be great too.

I don't want anyone to do all the work from me I already feel pretty shy for coming in here and asking for help with something i'm sure most people know how to do easily. Thank you for any help.

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was no limit on the while because 'true' is always true. You can put a test in there. This version will end the program if you press a button. Note this isn't real java, it's pseudocode to show you how what you've described could be structured.


 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds rather like something which is better implemented with a GUI with a button and a listener on that button.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He didn't actually ask for a button. I just gave him a way to stop the program, and give him a rare chance to see firmware too I envision something like press any key to end the loop. I'm assuming it's running on a pc and without a gui. But who knows
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why I said “rather like”.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is why I said “rather like”.

I don't know what that means but it sounds British.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very British
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!