break from for loop

This is a discussion on break from for loop within the C Programming forums, part of the General Programming Boards category; Code: does this work: for (j=height;j>0;j--) { for(i=0;i<width;i++) { if(array2[j][i] == objects[0]) { bottomRow = j; break; } } } ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question break from for loop

    Code:
    does this work:
    for (j=height;j>0;j--) 
    	{
    		for(i=0;i<width;i++) 
    		{
    			if(array2[j][i] == objects[0])
    			{
    				bottomRow = j;
    				break;
    			}
    		}
    	}
    When array[j][i] == objects[0] I want to break from the whole for loop.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for this and that
        for this and that
            if something
                flag is set
                break
        if flag is set
            break
    Something like that, since I assume you don't want a goto.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    I have tried this:
    Code:
    int topRow, bottomRow, leftCol, rightCol, flag=0, flag2=0;
    	for (j=0;j<height;j++) 
    	{
    		for(i=0;i<width;i++) 
    		{
    			if(array2[j][i] == objects[0])
    			{
    				topRow = j;
    				flag = 1;
    				break;
    			}
    		}
    		if(flag==1)
    			break;
    	}
    	
    	/*for (j=height-1;j>=0;j--) 
    	{
    		for(i=0;i<width;i++) 
    		{
    			if(array2[j][i] == objects[0])
    			{
    				bottomRow = j;
    				flag2=1;
    				break;
    			}
    		}
    		if(flag2==1)
    			break;
    	}*/
    
    	for(i=0;i<width;i++) 
    	{
    		array2[topRow][i] = 80;
    	}
    But once i uncomment the commented out bit it does NOT work properly. why?
    Last edited by taurus; 09-24-2009 at 03:45 PM. Reason: DOESNT

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Ok working for some reason now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 01:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM

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