There are a few problems with your question. The first is, the code snippet you provided doesn't perform what you intend it to perform:
foreach (type x in X)
{
foreach (type y in Y)
{
if(Z(x,y))
return true;
else
return false;
}
}
This will not be evaluated for all values in X nor all values in Y. Instead, the test Z(x,y)
will be executed only once, on the first element x0
in X and the first element y0
in Y. Then, according to this test, both loops will break (because return
exits the method).
The conventional way to do a "for all" test using loops would be something like:
foreach (var x in X)
{
foreach (var y in Y)
{
if(!Z(x,y))
return false;
}
}
return true;
Similarly, the conventional way to do "exists" test using loops would be:
foreach (var x in X)
{
foreach (var y in Y)
{
if(Z(x,y))
return true;
}
}
return false;
In C#, however, you can eliminate the need for loops using LINQ. So if you want to check whether a set of numbers X contains an even number, you can write:
return X.Any(x => x % 2 == 0);
EDIT: to clarify, after your edit: if you want to code the "for all x in X exists y in Y such that y+y==x", you can write something like:
foreach (var x in X)
{
if (!Y.Any(y => y+y == x))
return false;
}
return true;