A simple IF statement question [message #42513] |
Mon, 14 February 2005 05:55  |
andry_william
Messages: 3 Registered: December 2004
|
Junior Member |
|
|
Dear IDL user,
I am using IDL 6.1 on Linux SUSE. I am writing a simple code using the
IF statement and am wondering about the following result:
IDL> IF 1 THEN PRINT, '1' ELSE PRINT, 'None'
1
IDL> IF 2 THEN PRINT, '2' ELSE PRINT, 'None'
None
IDL> IF 19 THEN PRINT, '19' ELSE PRINT, 'None'
19
IDL> IF 24 THEN PRINT, '24' ELSE PRINT, 'None'
None
IDL> IF 0 THEN PRINT, '0' ELSE PRINT, 'None'
None
Am I wrong when I expect the IF statement to return always TRUE if the
condition is not 0 (I mean something like 1,2,3,4,....)?
Thanks for any comments.
Andry
|
|
|
Re: A simple IF statement question [message #42517 is a reply to message #42513] |
Mon, 14 February 2005 06:37   |
Benjamin Luethi
Messages: 22 Registered: December 2004
|
Junior Member |
|
|
Excerpt from the IDL help:
The definition of true and false for the different data types is as
follows:
- Byte, integer, and long: odd integers are true, even integers are false.
- Floating-point, and complex: non-zero values are true, zero values are
false.
The imaginary part of a complex number is ignored.
- String: any string with a nonzero length is true, null strings are false.
- Heap variables (pointers and object references): non-null values are
true,
null values are false.
In your case it's integer. So because 0, 2 and 24 are even they are
"false",
1 and 19 are "true" (odd).
Ben
On 14 Feb 2005 05:55:28 -0800, Andry William <andry_william@hotmail.com>
wrote:
> Dear IDL user,
>
> I am using IDL 6.1 on Linux SUSE. I am writing a simple code using the
> IF statement and am wondering about the following result:
>
> IDL> IF 1 THEN PRINT, '1' ELSE PRINT, 'None'
> 1
> IDL> IF 2 THEN PRINT, '2' ELSE PRINT, 'None'
> None
> IDL> IF 19 THEN PRINT, '19' ELSE PRINT, 'None'
> 19
> IDL> IF 24 THEN PRINT, '24' ELSE PRINT, 'None'
> None
> IDL> IF 0 THEN PRINT, '0' ELSE PRINT, 'None'
> None
>
> Am I wrong when I expect the IF statement to return always TRUE if the
> condition is not 0 (I mean something like 1,2,3,4,....)?
>
> Thanks for any comments.
>
> Andry
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
|
Re: A simple IF statement question [message #42518 is a reply to message #42513] |
Mon, 14 February 2005 07:11  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> Andry William writes:
>
>> I am using IDL 6.1 on Linux SUSE. I am writing a simple code using the
>> IF statement and am wondering about the following result:
>>
>> IDL> IF 1 THEN PRINT, '1' ELSE PRINT, 'None'
>> 1
>> IDL> IF 2 THEN PRINT, '2' ELSE PRINT, 'None'
>> None
>> IDL> IF 19 THEN PRINT, '19' ELSE PRINT, 'None'
>> 19
>> IDL> IF 24 THEN PRINT, '24' ELSE PRINT, 'None'
>> None
>> IDL> IF 0 THEN PRINT, '0' ELSE PRINT, 'None'
>> None
>>
>> Am I wrong when I expect the IF statement to return always TRUE if the
>> condition is not 0 (I mean something like 1,2,3,4,....)?
>
> Yes, you are wrong. :-)
>
> Here is an article you might want to read:
>
> http://www.dfanning.com/code_tips/bitwiselogical.html
It has been pointed out to me that the article is a bit
deficient in that it doesn't mention the LOGICAL_PREDICATE
compiler option. If you set:
COMPILE_OPT LOGICAL_PREDICATE
Then 0 is FALSE and everything else is TRUE. That probably
makes more sense to *everyone*! :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: A simple IF statement question [message #42522 is a reply to message #42513] |
Mon, 14 February 2005 06:49  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Andry William writes:
> I am using IDL 6.1 on Linux SUSE. I am writing a simple code using the
> IF statement and am wondering about the following result:
>
> IDL> IF 1 THEN PRINT, '1' ELSE PRINT, 'None'
> 1
> IDL> IF 2 THEN PRINT, '2' ELSE PRINT, 'None'
> None
> IDL> IF 19 THEN PRINT, '19' ELSE PRINT, 'None'
> 19
> IDL> IF 24 THEN PRINT, '24' ELSE PRINT, 'None'
> None
> IDL> IF 0 THEN PRINT, '0' ELSE PRINT, 'None'
> None
>
> Am I wrong when I expect the IF statement to return always TRUE if the
> condition is not 0 (I mean something like 1,2,3,4,....)?
Yes, you are wrong. :-)
Here is an article you might want to read:
http://www.dfanning.com/code_tips/bitwiselogical.html
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|