php - if condition at cycling number -
while having loop
0 1 2 3 4 5 0 1 2 3 ... there 2 directions instance number 4 forwarding 4 5 0 1 2 3 ... , backward 4 3 2 1 0 5 ... . how if algorithm detecting of forwarding or backward if values actual 2 next 3, or actual 5 next 0.
i'm looking simple if check
if (actual > next && ?jump last/first?) { forwarding } else { backward }
any ideas how detect forward or backward?
here's 1 way it:
$forward = ($actual < $next); if (abs($actual-$next) != 1) $forward = !$forward;
start off assuming forward if actual less next. check difference between actual , next - if it's not one, you're on boundary, result opposite of expect.
Comments
Post a Comment