I think your own method can be refined into something useful:
f1[a1_, a2_, a3_] := Module[{test, RM},
test = If[300 < # < 500, #, Return[{}, Module]] &;
RM = Table[test[a1 + a2 + a3 + i1], {i1, 1, 10}];
Total @ RM
]
Now:
f1[100, 101, 102]
f1[1, 2, 3]
f1[100, 195, 200]
3085
{}
{}
Note that I used a special syntax of Return to exit without error. I am assuming that you cannot simply test the end points of your Table range, but rather need to test every value generated by some function. Otherwise use a condition as rasher did, in one formulation or another.
If you want the function to return unevaluated you can achieve it with two small modifications:
f2[a1_, a2_, a3_] := Module[{test, RM},
test = If[300 < # < 500, #, Return[{}, Table]] &;
RM = Table[test[a1 + a2 + a3 + i1], {i1, 1, 10}];
Total @ RM /; RM =!= {}
]
f2[100, 101, 102]
f2[1, 2, 3]
f2[100, 195, 200]
3085
f2[1, 2, 3]
f2[100, 195, 200]
Note that Return is changed to exit from Table rather than the entire Module. Then a special form of Condition is used. See: Using a PatternTest versus a Condition for pattern matching
If failure to match is an uncommon event it is better to write the function to be faster in the common case where it does not exit or return unevaluated. For that you would leave the test until after the Table is generated, e.g.:
f3[a1_, a2_, a3_] := Module[{RM},
RM = Table[a1 + a2 + a3 + i1, {i1, 1, 10}];
Total @ RM /; 300 < Min[RM] && Max[RM] < 500
]
Speed comparison within a matching range:
Table[
Do[fn[100, 100, x], {x, 100, 280, 0.01}] // Timing // First,
{fn, {f1, f2, f3}}
]
{0.374, 0.375, 0.249}
i1as300<a1+a2+a3+i1<500. This is a simplified condition, but the original condition is much more complex – brama Apr 15 '14 at 18:15Tableiterator. – Mr.Wizard Apr 15 '14 at 19:46