วันอาทิตย์ที่ 23 ธันวาคม พ.ศ. 2550

Weird but may be good

Last week, I found my students writing a weird code for looping.
The problem ask them to sum number from 1 to n.
What they implement was as follow:

i = 1;
sum = 0;
while (n/i)
{
sum = sum + i;
i = i + 1;
}

I certainly sure that they misunderstand concept of condition
statement. However, this code produced correct results so that
they tell the others to write the same code.

If they truely understood what happend in the condition, n/i, which
is equivalent to n <= i, this would be a good idea.

ไม่มีความคิดเห็น: