For your existing code you need to Return the value of t rather than Printing it:
data = {10, 130, 300, 1000, 2000};
fun[t_] := Part[data, t];
comf[s_] := For[t = 1, t <= 4, t++, If[fun[t] <= s < fun[t + 1], Return @ t]]
Now:
comf[110] + 100
101
However, this code is very much not in the recommended style of Mathematica programming. See Alternatives to procedural loops and iterating over lists in Mathematica for some examples.
Alternatives
Perhaps the most direct simplification of the above code is to use Do in place of the manually incremented variable in For:
comf[s_] := Do[If[fun[t] <= s < fun[t + 1], Return @ t], {t, 4}]
You could also write it as a self-contained Piecewise function:
f1 =
Piecewise[
{{1, 10 <= # < 130},
{2, 130 <= # < 300},
{3, 300 <= # < 1000},
{4, 1000 <= # < 2000}}
] &;
f1 /@ {5, 17, 200, 300, 5000}
{0, 1, 2, 3, 0}
(The second argument of Piecewise can be used to determine what is returned when none of the conditions match; the default is zero.)
If you prefer to keep your values in data you might use something like LengthWhile:
f2[s_] := LengthWhile[data, # <= s &]
f2 /@ {5, 17, 200, 300, 5000}
{0, 1, 2, 3, 5}
A very efficient method is Interpolation with an InterpolationOrder of zero:
f3 = Interpolation[MapIndexed[{#, #2[[1]] - 1} &, data], InterpolationOrder -> 0];
Note however that this does not produce the same less-equal behavior as the others; it also warns you when the output is outside the interpolation range:
f3 /@ {5, 17, 200, 300, 5000}
InterpolatingFunction::dmval: Input value {5} lies outside the range
of data in the interpolating function. Extrapolation will be used. >>
InterpolatingFunction::dmval: Input value {5000} lies outside the
range of data in the interpolating function. Extrapolation will be
used. >>
{0, 1, 2, 2, 4}
See: How can the behavior of InterpolationOrder->0 be controlled?
rasher posted a method using Interval and IntervalMemberQ which I like, but which I believe can be written more efficiently. Specifically the inefficiencies are:
- computing the
Interval list every time the function is called
- not using the listability of
IntervalMemberQ
We can address both points with this:
With[{intv = Interval /@ Partition[data, 2, 1]},
f4 = Pick[index, IntervalMemberQ[intv, #]] &;
]
index = {1, 3, 4, 5};
f4 /@ {5, 30, 400, 5000}
{{}, {1}, {4}, {}}