Logo

Programming-Idioms

History of Idiom 43 > diff from v52 to v53

Edit summary for version 53 by vatsal:
[C] avoiding goto statement

Version 52

2020-03-03, 14:23:27

Version 53

2020-03-03, 14:24:32

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=pow(i,4);
			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]);
                        i=sizeof(m)/sizeof(*m);
			break;
		}
	}
}
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.