Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • D

Idiom #42 Continue outer loop

Print each item v of list a which is not contained in list b.
For this, write an outer loop to iterate on a and an inner loop to iterate on b.

Control flow jumping back to the beginning of the outermost loop
import std.stdio;
auto a = [1,2,3,4,5];
auto b = [3,5];

void main()
{
mainloop:
	foreach(v;  a){
		foreach(w; b){
			if(v == w) continue mainloop;
		}
		writeln(v);		 
	}
}
int *v = a;
while (v < a+N)
{
    int *w = b;
    while (w < b+M)
    {
        if (*v == *w)
            goto OUTER;
        
        ++w;
    }
    printf("%d\n", *v);
    
    OUTER: ++v;
}

N is the length of a.
M is the length of b.

Using goto is usually considered bad practice in C.

New implementation...
< >
programming-idioms.org