break statement

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 1
Reputation: aiyer is an unknown quantity at this point 
Solved Threads: 0
aiyer aiyer is offline Offline
Newbie Poster

break statement

 
0
  #1
Jan 16th, 2009
Hi,
I have this sample program where the compiler gives me error for the break statement.

The error is " misplaced break in afunction"

Could anyone help me understanding why the break statement gives an error? i understand that u can use break in a for , while loop etc., and exit early when required.

thanks for the help
  1. #include <iostream.h>
  2.  
  3. void afunction()
  4. {
  5. if(1)
  6. {
  7. cout<<"Err";
  8. break;
  9. }
  10. }
  11.  
  12. int main ()
  13. {
  14.  
  15. afunction();
  16. }
Last edited by Ancient Dragon; Jan 16th, 2009 at 6:23 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: break statement

 
0
  #2
Jan 16th, 2009
break statements can be used in looping constructs, and in switch/case blocks. They cannot be used to break out of an "if" block, or as an alternative means of returning from a function.

Also, <iostream.h> is non-standard. #include <iostream> instead.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: FrancisC07 is an unknown quantity at this point 
Solved Threads: 1
FrancisC07's Avatar
FrancisC07 FrancisC07 is offline Offline
Light Poster

Re: break statement

 
0
  #3
Jan 16th, 2009
i thing break is for switch case statement only..
"One more round and it's bottle to the ground"
-NOFX
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 950
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: break statement

 
0
  #4
Jan 16th, 2009
They're for abruptly "breaking" loops.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: break statement

 
0
  #5
Jan 16th, 2009
Originally Posted by aiyer View Post
#include <iostream.h>

void afunction()
{
if(1)
{
cout<<"Err";
break;
}
}

int main ()
{

afunction();
}

and exit early when required.
I believe that by placing a break statement you were trying to return back to the main() function.
Placing a
  1. return;//Instead of break might be what you are looking for
The above program just gives out an Err,
Probably because the above program is a 'test' program(or something of that sort)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: break statement

 
0
  #6
Jan 16th, 2009
yeah, try using return instead...
*mumbles something about multiple exit points*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC