My first C++ problem :(

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

My first C++ problem :(

 
0
  #1
Jul 17th, 2008
I have just started using C++ for the first time and I love it!!!

First problem I have encoutered proably a simple one but

  1. for (int a=0; a<10; a++) {
  2. for (int b=0; b<10; b++) {
  3. for (int c=0; c<10; c++) {
  4. if (b == 5) {
  5. break; beak; break; break;
  6. }
  7. }
  8. }
  9. }

Now the idea is if b is equal to 5 then it breaks out of the if loop, the for loop c, the for loop b, the for loop a.

Am I doing it right?

If not can someoone please correct me?

Thankyou, Regards X

PS: While on the topic what happens if I only wanted to break out of the if loop or if loop + for loop a or if loop + for loop a + for loop b. Thanks Again.
Last edited by OmniX; Jul 17th, 2008 at 11:15 am.
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: My first C++ problem :(

 
1
  #2
Jul 17th, 2008
>Now the idea is if b is equal to 5 then it breaks out of
>the if loop, the for loop c, the for loop b, the for loop a.
Nope. You can write as many breaks as you want, but only the first one will have any effect, and it'll break you out of the inner-most loop (the c loop).

>If not can someoone please correct me?
You have options here, but the best option is not to get into such a situation to begin with. If you need to break out of a nested loop, it's usually best (if you can manage it) to refactor the nested loops into a function and then use the return value to break:
  1. for ( int a = 0; a < 10; a++ ) {
  2. if ( !some_function() )
  3. break;
  4. }
  5.  
  6. ...
  7.  
  8. bool some_function()
  9. {
  10. for ( int b = 0; b < 10; b++ ) {
  11. for ( int c = 0; c < 10; c++ ) {
  12. if ( b == 5 )
  13. return false;
  14. }
  15. }
  16.  
  17. return true;
  18. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: My first C++ problem :(

 
0
  #3
Jul 17th, 2008
That would be a good idea use of functions but I am not able to in this problem

If i set it to a bool function then return false it will break me out completly, correct?

I require to just break out for loops a, b, c when required what would I do in that case?

Eg.
Break out of for loop a:
Break out of for loop a, b:
Break out of for loop a, b, c:

Thanks, Regards X
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: My first C++ problem :(

 
0
  #4
Jul 17th, 2008
Then use goto
  1. for (int a=0; a<10; a++) {
  2. for (int b=0; b<10; b++) {
  3. for (int c=0; c<10; c++) {
  4. if (b == 5)
  5. goto endloop;
  6. }
  7. }
  8. }
  9. endloop:
Last edited by invisal; Jul 17th, 2008 at 1:07 pm.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 609
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 8
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: My first C++ problem :(

 
0
  #5
Jul 17th, 2008
Thanks!!!

Very nice, so I can have multiple goto statements also?

a;
b;
c;

goto a;

goto b;

goto c;

Something like that?
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: My first C++ problem :(

 
1
  #6
Jul 17th, 2008
Of course you can have multiple of them. However, try to avoid using it as much as possible since most of the time goto can be replaced with a loop. For some case such as break multiple-level of loops, goto is inevitable.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: My first C++ problem :(

 
1
  #7
Jul 17th, 2008
>For some case such as break multiple-level of loops, goto is inevitable.
I wouldn't say inevitable, but goto can be the cleanest solution if you have deep nesting and can't refactor it. The alternative is to use flags and check them at each level, which is a "pure" structured solution, but it's almost always ugly as sin.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC