0

I have question how to make button to break while loop. I don't have idea that this method exist at all.

For example i have while loop:

while(x==10000){
  x++;
}

void onClick(view v){

    // break the while loop 
}

And i want to make a button, that when i clicked it i break the while loop and get x value. e.g x=9834. Ofc i want this button to most advanced function. I appreciate if you paste me code, how to make it. Thanks a lot for help!

1 Answer 1

3

Simply add another condition to break the while loop, that you control from your button :

boolean keepGoing = true;
while ((x==10000) && (keepGoing)) {
  x++;
}

void onClick(view v){
    keepGoing = false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

actually your code wouldn't work without adding sleep inside while.
@SergeyNeskoromny It would indeed depend of the speed at which the loop is executed as well as how fast the user clicks on the button. Having a while loop with a simple increment isn't an efficient way to wait for something, however this solution still fulfills the OP needs (interrupt a loop on click of a button)
Thanks a lot for help. It was really easy, that i didn't take it to consideration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.