Logo

Programming-Idioms

History of Idiom 43 > diff from v53 to v54

Edit summary for version 54 by programming-idioms.org:
Restored version 50

Version 53

2020-03-03, 14:24:32

Version 54

2020-03-03, 19:58:45

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Illustration

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Illustration
Imports
#include <stdio.h>
Imports
#include <stdio.h>
Code
int i,j;
for(i=0;i<sizeof(m)/sizeof(*m);i++)
{
	for(j=0;j<sizeof(*m)/sizeof(**m);j++)
	{
		if(m[i][j]<0)
		{
			printf("%d\n",m[i][j]);
                        i=sizeof(m)/sizeof(*m);
			break;
		}
	}
}
end:
Code
int i,j;
for(i=0;i<sizeof(m)/sizeof(*m);i++)
{
	for(j=0;j<sizeof(*m)/sizeof(**m);j++)
	{
		if(m[i][j]<0)
		{
			printf("%d\n",m[i][j]);
			goto end;
		}
	}
}
end:
Comments bubble
only works if m is allocated statically or on the stack, not if allocated in the heap.

edit: the statement above is misleading. It is referring to the use of sizeof() to set up the loops, and has nothing to do with using goto to break the loop. Using goto to break the loop will work as written, regardless of how the variables are allocated.
Comments bubble
only works if m is allocated statically or on the stack, not if allocated in the heap.

edit: the statement above is misleading. It is referring to the use of sizeof() to set up the loops, and has nothing to do with using goto to break the loop. Using goto to break the loop will work as written, regardless of how the variables are allocated.