hey,

i have a for loop that is infinite, it runs till i terminate the program.
inside of the loop there is a value(lets call it X) that keeps on changing all the time till i terminate the program.

when the program is executed i want to store the first x value once and keep it as constant and not changed while the loop is still running? (the storing of the x value should be inside the loop)

any suggestions would be greatly appreciated


thank you

You can use a boolean variable to tell you whether this is the first pass of the loop, eg

boolean isFirstPass = true;
while (... ) {
  ..
  if (isFirstPass) {
     // do stuff that you only want to do on first pass
     isFirstPass = false;
  }
}

this is very neat and very good thanks. this i going to help me a lot.

Sometimes your loop may look like...

while (true) {
}

This is a special case. Normally the loop is intended to keep going until a certain condition is met which is at an unknown time. Often times, you would find this in concurrent programming. In this case, you should add a if-else condition inside to break the loop.

while (true) {
  if (condition_met) { break; }  // get out of the loop immediately
}

PS: Please don't get confused though.

Edited 5 Years Ago by Taywin: n/a

You want the first iteration of the loop to be different from the rest of the iterations? James' boolean flag is one acceptable way to do that. The execution cost of checking that value and then setting firstPass to false at the end of each loop is negligible.
However, stylistically, checking an iteration flag each time should make you wonder if this is really the right way to do this. If this is a loop, and you know that the first time is the only time that's different, then maybe the first time shouldn't be in the loop. If you've written your code in a suitably modular fashion, this is easy to do. Something like this might work:

firstValue = getValue();
while (true)
{
  // do stuff
  currentValue = getValue();
  // do more stuff
}

At the end of the loop, you still have the first value on hand, and currentValue is gone away.

And your code to get the value is out in a method somewhere:

private Type getValue()   //I don't even know what sort of value you're getting
{
// get and return whatever value you need
}

Which is nice because it's obvious in the loop that that step is just getting a value and putting it in a place, so you don't have to think about the mechanics of getting the value when what you're really worried about is what you're doing with it.

Now your loop is a loop. It does the same thing over and over.


Another possible solution would be to just keep all of the values in storage. That's tremendously profligate, but it might be reasonable, depending on what sort of data we're talking about and what you might want to do with it later. If you think users might want access to the values after the first one, this might start to seem sensible.

Another solution would be to define small array and use simple math to store your first value in one place, and the working value in the other:

Type values[] = new Type[2];
int i = 0;
{
  values[i] = getValue();
  ...
  i%=1;
  i++;
 }

This is a little obscure in its purpose, and you would have to include some comments, but it's another way to get what you're looking for. I'd prefer to just do the first getValue outside the loop, since that's what you're actually trying to do, but if you want to do it in the loop, this is a little bit cute. (I'd always prefer an array lookup to an if, given the choice, and you usually do have the choice)

Anyway, there's a few ideas for you. Have fun.

Edited 5 Years Ago by jon.kiparsky: n/a

or you could declare a final variable, which you give a value the first time your loop runs.
final variables, once given a value, can never be changed to another value.

thanks a lot everybody this really did help me.
THANK YOU

This question has already been answered. Start a new discussion instead.