-3

How can I stop a while loop from looping when an integer variable gets a value. By value I mean any value not just a predefined value.

int foo; while(foo = ){ // some code }

right when foo gets value i would like the while loop to stop.

Fence_rider
  • 163
  • 1
  • 14
  • 3
    you [`break;`](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html) – jmj Apr 17 '14 at 21:46
  • 1
    `while (foo != value) {` – Dawood ibn Kareem Apr 17 '14 at 21:47
  • 1
    Uh, you wouldn't be able to use `foo` if it were not initialized (which means it already has a value)! Please reword your question more clearly – fge Apr 17 '14 at 21:47
  • If that other value is in the variable `target`, have `while (foo != target) {...}` – AntonH Apr 17 '14 at 21:47
  • We need a bit more context on that. Perhaps it is better to run the loop until it is *possible* to set the value of foo, then set the value of foo after we know we are ready to break out? – nanofarad Apr 17 '14 at 21:47
  • possible duplicate of [How do I exit a while loop in Java?](http://stackoverflow.com/questions/7951690/how-do-i-exit-a-while-loop-in-java) – jmj Apr 17 '14 at 21:47
  • Is it going to stop if foo is 0? Is 0 a valid "value"? – digit plumber Apr 17 '14 at 21:47
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Hovercraft Full Of Eels Apr 17 '14 at 21:48
  • 2
    Maybe the Java tutorials would be a good port of call here, particularly the section on loops. – Dawood ibn Kareem Apr 17 '14 at 21:48

3 Answers3

0
int foo = 0;
while (foo != value) {
    // code here
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
0

Two ways:

while(foo != ...) {
    foo = ...
}

When foo reaches the value you'd like, the loop will finish.

while(true) {
    foo = ...
    if(foo = ...) {
        break;
     }
 }

This loop will always continue unless foo reaches the value you'd like to exit on. break will exit any loop, so in this case when foo equals your value, it will exit.

Justin C
  • 333
  • 1
  • 7
0

I am interpreting "By value I mean any value not just a predefined value" as that you want the loop to break as soon as foo gets any sort of value.

Due to the rules of local variables, any assignment to foo must cause foo to get a value (it being primitive, namely int).

Therefore, either no loop actually will happen and you simply assign foo, or you'll have some conditional that when taken assigns foo. The former is a trivial case of assigning to foo. The latter can be written as:

int foo = 0; //dummy value
while (true){ 
  if (/*ready to assign*/){
       foo = /*whatever value you assign to it*/
       break;
    }
}

The dummy value is used as compilers don't do very much static analysis and may complain about the value being uninitialized.

nanofarad
  • 40,330
  • 4
  • 86
  • 117