Welcome!

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

SignUp Now!

WAD can somebody please explain how this is not a bug? what am i missing?

Jul
530
10
First off: Why is the second loop's first value 4 and not 1? Why is it not re-initialized as part of the do?

Second off: Why is enddo an unknown command? I literally copied the same code

If I change the if them to use gotos instead of parens, this error goes away.


Stuff like this is why I got into the 25 year habit of never using parens with if statements, which resulted in some ugly code.

1728319016727.webp




Code:
@Echo off

set num_seconds=3

do second = 1 to %num_seconds% by 1
        echo first loop: %second%
         delay 1
enddo


        if (0 == 1) (
                 delay %num_seconds%
        ) else (
                do secondagain = 1 to %num_seconds% by 1
                        echo second loop: %secondagain%
                         *delay 1
                enddo
        )


<12:36p> <9%> C:\bat>test-tcc-bug-enddo.bat
first loop: 1
first loop: 2
first loop: 3
second loop: 4
  Unknown command: 'enddo'
``
 
First off: Why is the second loop's first value 4 and not 1? Why is it not re-initialized as part of the do?

Second off: Why is enddo an unknown command? I literally copied the same code

If I change the if them to use gotos instead of parens, this error goes away.


Stuff like this is why I got into the 25 year habit of never using parens with if statements, which resulted in some ugly code.
 
You're putting a (multiline) DO inside an IF. Remember that IF is a single-line command. All those nasty parentheses are just mashing the entire second part of your batch file into one big line. Variable expansion of that line happens all at once... probably not what you want.

Rewrite it to use IFF / ELSE / ENDIFF. You and the parser will both be much less confused.

Alternatively, you could use the single-line form of DO. But that's just more stinkin' parentheses.
 
Like Charles said - it's not a bug.

Well, OK, it is a bug, but it's a bug in the CMD IF command. OK, it's actually two bugs in the CMD IF command, and TCC is faithfully emulating them. (First rule of TCC parsing questions - if it doesn't make sense, it's almost certainly for CMD compatibility.)

First, CMD treats an IF as a single command line, by appending all of the following lines until it gets a matching trailing ')'. This means that your DO statement is combined into a single line, which renders the "ENDDO" pointless. And the ECHO is executed after DO exits.

Second, CMD will throw away everything following a non-matching IF clause (ignoring command separators and conditional commands). This isn't invoked with your sample batch file, but it will bite you once you try to something a little more complex.

The TCC IFF command does not suffer from either of those bugs / limitations.
 
Adding: Your 25-year aversion to using IF with parentheses is, IMHO, perfectly correct. Don't do that. Use IFF instead.
 
Adding: Your 25-year aversion to using IF with parentheses is, IMHO, perfectly correct. Don't do that. Use IFF instead.

I realllllllllllllllly needed to hear this 25 years ago, lol

Thank you for the explanation! I'm finally starting to more thoroughly understand what I should have, really, probably in the 1990s.

Rexx: I will keep that in mind! "If (weird) then { probably emulating CMD.exe }"
 
So as an update, I’m running into situation where if with parenthesis works, and iff then endiff does not work....

But it’s all within a multiline do ( ) , so am I correct to assume it’s the same reasoning here?
 
Back
Top