943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1224
  • C++ RSS
Jul 17th, 2008
0

My first C++ problem :(

Expand Post »
I have just started using C++ for the first time and I love it!!!

First problem I have encoutered proably a simple one but

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Jul 17th, 2008
1

Re: My first C++ problem :(

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 17th, 2008
0

Re: My first C++ problem :(

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
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Jul 17th, 2008
0

Re: My first C++ problem :(

Then use goto
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 17th, 2008
0

Re: My first C++ problem :(

Thanks!!!

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

a;
b;
c;

goto a;

goto b;

goto c;

Something like that?
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Jul 17th, 2008
1

Re: My first C++ problem :(

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.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 17th, 2008
1

Re: My first C++ problem :(

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: do while loop
Next Thread in C++ Forum Timeline: Creating charts in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC