labeled breaks

This is a discussion on labeled breaks within the C++ Programming forums, part of the General Programming Boards category; Here is some code in java I want to put in C++ Code: out: for(int u=0;u<N;u++) { Arrays.fill(dist,20000); int source ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    5

    labeled breaks

    Here is some code in java I want to put in C++

    Code:
    	out:
    			for(int u=0;u<N;u++)
    			{
    				
    				Arrays.fill(dist,20000);
    				int source = u;
    				dist[source] = 0;
    				
    				for(int i=0;i<N;i++)
    				{
    					for(int k=0;k<edges;k++)
    					{
    						int sol = dist[ t[k] ] + c[k];
    						int v = f[k];
    						if( dist[v] > sol  )
    							dist[v] = sol;
    						 
    					}
    				}
    				for(int k=0;k<edges;k++)
    				{
    					if( dist[ f[k] ] > dist[ t[k] ] + c[k])
    					{
    						negcycle = true;
    						break out;
    					}
    				}
    			}
    Since c++ dont have labeled breaks, how would I code the above?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    would this be fine?
    Code:
    		for(int u=0;u<N;u++)
    			{
    				memset(dist,20000,sizeof(dist));
    				int source = u;
    				dist[source] = 0;
    				
    				for(int i=0;i<N;i++)
    				{
    					for(int k=0;k<edges;k++)
    					{
    						int sol = dist[ t[k] ] + c[k];
    						int v = f[k];
    						if( dist[v] > sol  )
    							dist[v] = sol;
    						 
    					}
    				}
    				for(int k=0;k<edges;k++)
    				{
    					if( dist[ f[k] ] > dist[ t[k] ] + c[k])
    					{
    						negcycle = true;
    					    goto outside;
    					}
    				}
    			}
    			outside:

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It would. However, a more respected way is to activate the termination condition for the outer loop and then break then inner. In your case, set u to N before breaking.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by soljaragz
    would this be fine?
    Code:
     goto outside;
    It's best to avoid goto statements whenever possible. I'd try something like...

    Code:
    for(int u=0;u<N;u++)
    			{
    				
    				//snip
    
                                    bool bOut = false;
    				for(int k=0;k<edges;k++)
    				{
    					if( dist[ f[k] ] > dist[ t[k] ] + c[k])
    					{
    						negcycle = true;
                                                    bOut = true;
    						break;
    					}
    				}
    
                                    if(bOut)
                                            break;
    			}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > memset(dist,20000,sizeof(dist));
    This isn't a replacement for arrays.fill

    Rynoon's answer seems OK, but isn't negcycle also the same flag?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Breaking nested for loops is one of the few uses of goto that is considered acceptable, although whether it is a good choice is debatable. I personally would first try to factor out another function if it makes sense. If not, then I'd probably use something similar to rynoon's example.

    Incidentally, I believe something similar to a labeled break is being considered for the next C++ standard, although I don't recall how likely it is to get in.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    30
    rycoons answer is the only clean solution to this. labeled breaks are just not something for c++. break just indicates to quit the loop, where java does much of the memory management for you, in c++ your responsible for it yourself so labeled break statements could lead to serius problems. I dont think they ever gonna put it in the next standard

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I've no idea what you mean, DV. Even goto respects destruction of objects when leaving scopes (and I pity the compiler writers for having to implement this).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C App to add line breaks to a text file
    By sari in forum C Programming
    Replies: 2
    Last Post: 07-09-2009, 11:45 PM
  2. extra line breaks from recv?
    By xconspirisist in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2005, 02:00 AM
  3. sentence word breaks...
    By aker_y3k in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2003, 12:52 PM
  4. Breaks
    By wuzzuppy_123 in forum C++ Programming
    Replies: 6
    Last Post: 05-09-2003, 01:31 PM
  5. Replies: 5
    Last Post: 03-01-2003, 10:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21