After each call to 'search', I would add:
if (breaknow)
break;