Logo

Programming-Idioms

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

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
main()  {
  var a = [1, 2, 3];
  var b = [2, 3];

  outer: for (var v in a) {
    for (var w in b) {
      if (w == v) continue outer;
    }
    print(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;
}
#include <iostream>
auto a = {1,2,3,4,5};
auto b = {3,5};

for (auto va: a){
    for (auto vb: b){
        if (va==vb) goto OUTER;
    }
    std::cout << va << '\n';
    OUTER: continue;
}
int gb = 0;
foreach (int v in a)
{
  foreach (int w in b)
  {
    gb = w;
    if (w == v)
      break;
  }
  if (gb == v)
    continue;
  System.Console.WriteLine(v);
}
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);		 
	}
}
  outer: do i=1,size(a)
     do j=1,size(b)
        if (a(i) == b(j)) cycle outer
     end do
     print *,a(i)
  end do outer
mainloop:
	for _, v := range a {
		for _, w := range b {
			if v == w {
				continue mainloop
			}
		}
		fmt.Println(v)
	}
sequence_ [ print v | v <- a, [ u | u <- b, u == v] == [] ]
OUTER:
for (var i in a) {
   for (var j in b) {
      if (a[i] === b[j]) {
         continue OUTER;
      }
   }
   console.log(a[i] + " not in the list");
}
mainloop: for(int v:a){
	for(int w:b){
		if(v==w)
			continue mainloop;
	}
	System.out.println(v);
}
$array_1 = [1,2,3,4,5];
$array_2 = [3,4];

foreach ($array_1 as $a) {
    foreach ($array_2 as $b) {
        if ($b == $a) {
            continue 2;
        }
    }
    echo $a
}
  for v in a do
    begin
      for  w in b do
        if (v = w) then
          break;
      if (v = w) then
        Continue;
      writeln(v);
    end;              
OUTER:
for my $v (@a) {
   for my $check (@b) {
      next OUTER if $v == $check;
   }
   print "$v not in the list\n";
}
      
for v in a:
    try:
        for u in b:
            if v == u:
                raise Exception()
        print(v)
    except Exception:
        continue
for v in a:
  keep = True
  for w in b:
    if w == v:
      keep = False
      break
  if keep:
    print(v)
a.each do |v|
  catch :matched do
    b.each do |u|
      throw :matched if v == u
    end
    puts v
  end  
end
'outer: for va in &a {
    for vb in &b {
        if va == vb {
            continue 'outer;
        }
    }
    println!("{}", va);
}

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