Click here to Skip to main content
12,697,103 members (26,481 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C MFC VisualC++
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 20-Jun-12 23:12pm
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.
Wes Aday 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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


[edit]Typo: "1=1" instead of "1==1". Thanks johannesnestler! - OriginalGriff[/edit]
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if you neeed compact code :)

while(!condition)
{
   xxx();
}


Set condition=true in button's handler
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

// 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)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

u have to discribe the particular key name...in the while statement
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 21 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100