In the spirit of trying to foster understanding let's take a look at an interesting problem.
The task our novice programmer had to solve was checking that two variables were equal and doing something if they were. Later in the program each variable was tested for being a zero. The raw essence of the program was:
if(a==b)alert("a equals b");
if(a==0)alert("a is zero");
if(b==0)alert("b is zero");
You can try alternative logical arrangements but this is arguably the simplest.
This code worked perfectly for a while and then a problem came to light.
The first statement displayed "a equals b"
The second statement displayed "a is zero".
The third statement did not display "b is zero"
So a and b are not equal but a is zero and c is a zero!
What is going on?
What values of a and b behave like this?
Turn to the next page when you are ready to find out.
- Next >>