In some languages, the assignment operator, "=", not only assigns a
value to a variable, it also returns that value as the expression's
value itself. This is to allow things like
a=b=c=42;
So in the expression
red_button_pressed = 1
The value 1 is assigned to the variable red_button_pressed, and
returns a "1" as the value for the expression. Unfortunately, this
is also interpreted as "true" for conditional statements like "if"
and "while" etc.
The real intent of the code is to use the == operator, which returns
true if the two operands are the same. So the *extremely* common bug
of using = rather than == causes entirely unexpected consequences ..
launching a missile.
// World's last bug
if (red_button_pressed = 1)
launch_missile();
The solution many programmers got into the habit of is to reverse the
variable and the value to be:
if (1 = red_button_pressed)
launch_missile();
This causes an error within the compiler because "1" is a literal and
cannot be assigned to.
-- Owen
Owen Densmore http://backspaces.net
On Oct 25, 2006, at 9:44 PM, David Breecker wrote:
> I was about to say: can someone translate this joke into English?
> (It
> looks funny, but...)
>
> ----- Original Message -----
> From: "Owen Densmore" <[EMAIL PROTECTED]>
> To: "The Friday Morning Applied Complexity Coffee Group"
> <[email protected]>
> Sent: Wednesday, October 25, 2006 8:11 PM
> Subject: Re: [FRIAM] div. zero bugs?
>
>
>>>> // World's last bug
>>>> if (red_button_pressed = 1)
>>>> launch_missile();
>>>
>>> I thought that was pretty funny ^_^
>>
>> .. for all 7 of us who understood it!
>>
>> -- Owen
>>
>> Owen Densmore http://backspaces.net
>>
>>
>>
>>
>> ============================================================
>> FRIAM Applied Complexity Group listserv
>> Meets Fridays 9a-11:30 at cafe at St. John's College
>> lectures, archives, unsubscribe, maps at http://www.friam.org
>>
>>
>
>
> ============================================================
> FRIAM Applied Complexity Group listserv
> Meets Fridays 9a-11:30 at cafe at St. John's College
> lectures, archives, unsubscribe, maps at http://www.friam.org
============================================================
FRIAM Applied Complexity Group listserv
Meets Fridays 9a-11:30 at cafe at St. John's College
lectures, archives, unsubscribe, maps at http://www.friam.org