Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C MFC VC++
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 22:12pm
Comments
Jochen Arndt at 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 at 21-Jun-12 9:16am
   
You have already asked this and acknowledged the question as solved. Why are you asking again?
Member 9102753 at 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 at 21-Jun-12 4:58am
   
a minor typo: 1=1 should be 1==1 or true or just 1
OriginalGriff at 21-Jun-12 5:13am
   
Oops! :O
Fixed.
Soheera at 21-Jun-12 6:36am
   
shouldn't the condition be (key != 1)?
OriginalGriff at 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 at 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 Smile | :)
 
while(!condition)
{
   xxx();
}
 
Set condition=true in button's handler
  Permalink  
v2
Comments
Jochen Arndt at 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 Smile | :) :
- 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
0 Sergey Alexandrovich Kryukov 458
1 OriginalGriff 294
2 George Jonsson 258
3 CPallini 160
4 Shemeemsha RA 140
0 OriginalGriff 5,985
1 Sergey Alexandrovich Kryukov 5,341
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 21 Jun 2012
Copyright © CodeProject, 1999-2014
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