Programming-Idioms

History of Idiom 43 > diff from v40 to v41

Edit summary for version 41 :
[Csharp] Fixed variable misnaming

Version 40

2019-09-26, 15:33:53

Version 41

2019-09-26, 15:35:03

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
Code
bool keepLooping = true;

for(int i = 0; i < m.length && keepLooping; i++)
{
	for(int j = 0; i < m[i].length && keepLooping; j++)
	{
		if(m[i][j] < 0)
		{
			Console.WriteLine(m[i][j]);
			keepLooping = false;
		}
	}
}
Code
bool keepLooping = true;

for(int i = 0; i < m.length && keepLooping; i++)
{
	for(int j = 0; j < m[i].length && keepLooping; j++)
	{
		if(m[i][j] < 0)
		{
			Console.WriteLine(m[i][j]);
			keepLooping = false;
		}
	}
}
Comments bubble
Trying to avoid GOTO with this implementation.
Comments bubble
Trying to avoid GOTO with this implementation.