Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

WAD Difference Between IF and IFF

Jun
552
4
I expected that the IF and IFF commands would properly interpret the values 0 and 1 to be FALSE and TRUE, respectively. When I tested it with IFF, it worked.

TCC(30.00.18): C:\>iff 0 then echo TRUE & else & echo FALSE & endiff
FALSE

TCC(30.00.18): C:\>iff 1 then echo TRUE & else & echo FALSE & endiff
TRUE

So I could not understand why my code that used IF was failing with error messages -- until I tried the same simple cases.

TCC(30.00.18): C:\>if 0 echo FALSE
Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...] command

TCC(30.00.18): C:\>if 1 echo TRUE
Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...] command

With IFF, I can use code like this:

IFF %@REGEX[...] THEN

or

IFF %@LENGTH[...]

but similar commands with IF do not work.
 
Just a selling point of IFF.

Because IFF requires THEN it's easy to isolate the conditional expression ... not so easy with IF (and I'll bet, pretty complicated).
 
Good point. I guess I should consider recognizing a number as a Boolean value as a special feature of IFF rather than the lack of that feature as a bug in IF.

I use an alias IFTEST for testing conditional code before I put it into scripts.

TCC(30.00.18): C:\>alias iftest
iff %$ then echo TRUE & else & echo FALSE & endiff

To keep myself from getting tripped up, I should rename that alias to IFFTEST for testing IFF and create a new one called IFTEST for testing IF.

TCC(30.00.18): C:\>alias iftest
if %$ echo TRUE & if not ( %$ ) echo FALSE

Now iftest 1 gives me an error message (actually two, one from each IF command).
 
A further thought. If the argument to IF is a number, wouldn't it have to be followed by EQ, GT, etc. to be a Boolean expression? Anything else would indicate that it is a standalone Boolean value.

Even if it could be done without enormous difficulty, it certainly is not a high priority issue. However, it would be good to add a note to the help entries for IF and IFF. I thought that I read somewhere in the help that a value of 0 had the Boolean value FALSE and any other number, the Boolean value TRUE. But maybe I'm thinking of another programming language (e.g., Pascal) and not TCC.
 
I found in the help that @EVAL treats numbers as described above when the not operator (!) signals a Boolean calculation. The result is returned as 0 or 1 as FALSE or TRUE, respectively.

TCC(30.00.18): C:\>echo %@eval[ ! 0 ]
1

TCC(30.00.18): C:\>echo %@eval[ ! 1 ]
0

TCC(30.00.18): C:\>echo %@eval[ ! 10 ]
0

I think I'm understanding this correctly.
 
I discovered that I had to remove the parentheses in the second IF command in my alias IFTEST. The help file is actually quite clear about the limited situations in which parentheses can be used.

TCC(30.00.18): C:\>alias iftest
if %$ echo TRUE & if not %$ echo FALSE
 
Not a bug.

Several things to consider here:

1. IF and IFF are completely unrelated; expecting them to share the same syntax is going to land you in a world of hurt.
2. IF's syntax is chained to CMD for compatibility reasons. If you try that "if 0 echo false" syntax with CMD, it will fail ("echo was unexpected at this time").
3. IFF doesn't have to worry about CMD compatibility, so it can be smarter about parsing the line.
4. Use IF if you want CMD compatibility; use IFF otherwise.
 

Similar threads

Back
Top