# Convert C FOR (delta > t) to While or Do Until

The problem is during one pass through the test code where I copy/paste/syntax corrected the CONST input data the C code’s return value is 3 whereas my code returns 1 with identical inputs.

C code:
punycode_uint delta, punycode_uint numpoints, int firsttime )
{
punycode_uint k, r, t;
delta = firsttime ? delta / damp : delta >> 1;
delta += delta / numpoints;
for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base) {
delta /= base - tmin;
}
return (base * delta) / (delta + skew) + k;
}

My Xojo code:

``````Var result As UInt32
Var ctr As UInt32 = 0
If firstCall Then
Delta = Delta \ DAMP
Else
Delta = Delta \ 2
End If
Delta = Delta + (Delta \ numberOfPoints)
Var cmp As UInt32 = ((BASE - T_MINIMUM) * T_MAXIMUM) \ 2
While Delta < cmp
Delta = Delta \ (BASE - T_MINIMUM)
ctr = ctr + BASE
Wend
result = (BASE * Delta) \ (Delta + SKEW) + ctr
Return result``````

I’ve tried Delta >= cmp as well. What termination condition should the While loop be using?

A ‘C’ `for` loop is a shorthand for a certain type of `while`.

``````for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base)
{
delta /= base - tmin;
}
``````

Unfolds to:

``````k = 0;
while (delta > ((base - tmin) * tmax) / 2) {
delta /= base - tmin;
k += base;
}
``````

Something like this:

``````While Delta > ((BASE - T_MINUM) * T_MAXIMUM) \ 2
``````