Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Difference between break; and continue;
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ownageprince




PostPosted: Wed Jan 03, 2007 10:31 pm   Post subject: Difference between break; and continue;

Sorry if its a dumb question...i tried them out and i think that they do same thing...i tried them in if structures and switch...so can anyone please explain the difference because why make two separate commands that do the same thing right?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jan 04, 2007 12:14 am   Post subject: (No subject)

In a loop, continue will advance immediately to the next step through the loop.
r.3volved




PostPosted: Thu Jan 04, 2007 1:53 pm   Post subject: (No subject)

....break will break you out of the loop and run the code that follows it

try this and see the difference:
code:

int x = 0;
for( x; x != 10; ++x ) {
   if( x == 5 ) break;
}
cout << x;


code:

int x = 0;
for( x; x != 10; ++x ) {
   if( x == 5 ) continue;
}
cout << x;
ericfourfour




PostPosted: Thu Jan 04, 2007 5:07 pm   Post subject: (No subject)

I think he asked for the purpose in an if and case statement. I'm not really knowledgeable on this front so I'll leave it up to someone else to assist you.
Monstrosity_




PostPosted: Fri Jan 05, 2007 6:47 pm   Post subject: Re: Difference between break; and continue;

ownageprince wrote:
Sorry if its a dumb question...i tried them out and i think that they do same thing...i tried them in if structures and switch...so can anyone please explain the difference because why make two separate commands that do the same thing right?

I suggest you turn up the warning level on your compiler because this is a constraint violation. In any case, I'll give you the definition in C, since you failed to specify which language you are using.

ISO C9899:1990 wrote:

Constraints
A continue statement shall appear only in or as a loop body.

Semantics
A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

<examples snipped>

Constraints
A break statement shall appear only in or as a switch body or loop body.

Semantics
A break statement terminates execution of the smallest enclosing switch or iteration statement.
ownageprince




PostPosted: Fri Jan 05, 2007 8:10 pm   Post subject: (No subject)

r.3volved wrote:

try this and see the difference:
code:

int x = 0;
for( x; x != 10; ++x ) {
   if( x == 5 ) break;
}
cout << x;


code:

int x = 0;
for( x; x != 10; ++x ) {
   if( x == 5 ) continue;
}
cout << x;


so in the continue one it will continue the for loop when x hits 5 and will display 10 after the loop is done but in the break one it will stop right away and then go to the cout<<x; right?

if so then why put continue or is there another purpose to it?

also am using microsoft visual C++ 6.0
ty for replys all!
ownageprince




PostPosted: Fri Jan 05, 2007 8:12 pm   Post subject: (No subject)

hang on a sec:

for(int x=0;x<10;x++)
{
if(x==5)
continue;
cout<<x<<endl;
}
this will cout x everytime until it hits that if statement right? because continue will tell it to go to the starting the for loop and skip the following in the loop?
wtd




PostPosted: Fri Jan 05, 2007 8:13 pm   Post subject: (No subject)

What does the following output?

code:
for (int i = 0; i < 10; i++)
{
    if (i % 2 == 0)
    {
        continue;
    }

    std::cout << i << std::endl;
}
Sponsor
Sponsor
Sponsor
sponsor
ownageprince




PostPosted: Sat Jan 06, 2007 9:25 am   Post subject: (No subject)

does it output:
1
3
5
7
9
wtd




PostPosted: Sat Jan 06, 2007 9:33 am   Post subject: (No subject)

If you understand break and continue, you'll be able to give a confident answer. Smile
md




PostPosted: Sat Jan 06, 2007 4:55 pm   Post subject: (No subject)

Not to belittle anyone here.. but has anyone tried oh say running the code? Instead of guessing at the output of something so trivial try running it and see what happens!
ownageprince




PostPosted: Sat Jan 06, 2007 7:44 pm   Post subject: (No subject)

lolz ty md buh understand first then type code or what the heck...anyways i took your advice and ran it and i was right
ty all who replied. appreciate that!
TheFerret




PostPosted: Sat Jan 06, 2007 8:18 pm   Post subject: (No subject)

ownageprince wrote:
lolz ty md buh understand first then type code or what the heck...anyways i took your advice and ran it and i was right
ty all who replied. appreciate that!


Posting like this makes you seem like an idiot and make less people want to help you since what you are saying is unclear, it has a rude quality about it and is spoken in "l33t speak" which makes your point even more unclear... It would be wise to use proper grammar and typing skills here in the future so people will help you more willingly...
Andy




PostPosted: Mon Jan 08, 2007 10:15 am   Post subject: RE:Difference between break; and continue;

err how is that l33t speak?
md




PostPosted: Mon Jan 08, 2007 5:18 pm   Post subject: RE:Difference between break; and continue;

That's not 1337; that's just horrible writing.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: