bug with integer i and d confusion in forloop