Click here to Skip to main content
15,940,218 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi guys
in my code i am using one infinite loop while(1)
{
xxx;

}
But now i need to break this loop when i press one button ? how to do that? please help me in that ....Thanks for ur valuable comments
Posted
Comments
Jochen Arndt 21-Jun-12 4:33am    
This seems to be similar to your question at http://www.codeproject.com/Questions/407747/while-loop-should-run-till-i-press-another-button. Please don't ask the same multiple times.
[no name] 21-Jun-12 9:16am    
You have already asked this and acknowledged the question as solved. Why are you asking again?
Member 9102753 21-Jun-12 22:57pm    
sorry for asking it again i am unhappy with the previous question. sorry for repeating the question

The break statement causes execution of the loop to end immediately:
C++
while(1==1)
   {
   xxx();
   if (condition)
      {
      break;
      }
   }


[edit]Typo: "1=1" instead of "1==1". Thanks johannesnestler! - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
johannesnestler 21-Jun-12 4:58am    
a minor typo: 1=1 should be 1==1 or true or just 1
OriginalGriff 21-Jun-12 5:13am    
Oops! :O
Fixed.
Soheera 21-Jun-12 6:36am    
shouldn't the condition be (key != 1)?
OriginalGriff 21-Jun-12 6:50am    
Depends how how he detects keypress. Since he doesn't give us any idea, it's safest just to use a generic "condition" and let him sort it out.
Soheera 3-Jul-12 6:21am    
yeah break will do the job in any case
if you neeed compact code :)

C++
while(!condition)
{
   xxx();
}


Set condition=true in button's handler
 
Share this answer
 
v2
Comments
Jochen Arndt 21-Jun-12 6:47am    
This requires message processing inside the loop to ensure that the button's handler is called.
// But now i need to break this loop when i press one button

There are at least two variants :) :
- the loop and the key-watcher are in different threads (wather makes the break condition)
- the loop does process messages for its (main) thread as well (processing makes the break condition)
 
Share this answer
 
u have to discribe the particular key name...in the while statement
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900