1

Yesterday while debugging some code I eventually found the culprit line:

 for (int i=0; i <- b; i++) 

instead of:

 for (int i=0; i <= b; i++) 

Why does this compile? I could understand if it had been mistyped as:

for (int i=0; i <-b; i++)
3
  • 6
    the space inbetween doesn't really matter at all in this case. for the compiler, options 1 and 3 are the same. technically, you can eliminate all spaces in these lines except the one between int and i. Commented Jul 12, 2020 at 19:37
  • Because the compiler allows us to put an operator before a numeric type variable. Commented Jul 12, 2020 at 19:39
  • 2
    So many answers to say that i <- b equals i < (-b). Is 5 < -10 ? No. But 5 < -(-10) ? Yes. Commented Jul 12, 2020 at 19:56

4 Answers 4

2

Explaining the more abstract form, in cSharp there is no token '<-', but two tokens '<' and '-'.

When the lexical analyzer reads the characters it checks if the next character produces any tokens that it can recognize (look-ahead). If not, it passes to the parser the last token that it was able to identify and proceeds to the next token identification.

Step by step:

  • < -> Token identified. Wait for the next entry (look-ahead)
  • <- -> Unidentified token, pass < to the parser and keep the -
  • - -> Token identified. Wait for the next entry (look-ahead)
  • -b -> Unidentified token, pass - to the parser and keep the b
  • b -> Token identified. Wait for the next entry (look-ahead)
  • b; -> Unidentified token, pass b to the parser and keep the;
  • And so on...

In the end the parser translates this as "i < (-b)".

Obviously it has a deeper and more detailed explanation of how the lexical analyzer and the parser work, but I think it is not necessary to explain to answer your question.

In the summary, it makes no difference whether the code was written as "i <- b" or "i <-b" or "i < - b", because for the lexical analyzer of cSharp, space does not matter "i<-B".

References:

Sign up to request clarification or add additional context in comments.

Comments

0

It compiles because it is syntactically correct. It means i < (b*-1).

Comments

0

White space does not define scope in C#, so your statement compiles.

for 
(
            int 
i
=
        0; 
i 
            <    -

             b
                                 ;
              i
                ++
)

would compile too, but you'd be a monster if you let it. The unary - operator, like any other, doesn't necessarily need to be next to its operand to be valid.

2 Comments

@Stefan I'm aware. I answered why - because it's not an error according to the compiler, because the compiler does not consider white space when parsing the statement. The OP assumes that the negation operator must not have white space before its operand, as evidenced by the "I could understand if it had been typed as..." follow on, which is incorrect.
... hmm, I think you are right. Although: you really put the focus on the white space - without the -, it's confusing, sorry.
0

It is not. Have a look at this snippet:

using System;
                    
public class Program{
    public static void Main(){
        int i=0, j=-5;
        for(;i<-j;i++)
            Console.WriteLine("i: {0}, j: {1}", i, j);
    }
}

The end result would be:

i: 0, j: -5                                                                                                                 
i: 1, j: -5                                                                                                                 
i: 2, j: -5                                                                                                                 
i: 3, j: -5                                                                                                                 
i: 4, j: -5  

Its simple math. -(-) is +. So -(j) = -(-5) = 5.

11 Comments

I would say your code snippet is not written in C#
But you also know here there is no diffrence in many languages c, c#, java, php ... and the other c-based languages.
Still your answer does not address OP's question "why does this compile". Moreover it claims for some reason that "It is not".
Its really ridiculous. My code is totally right :( Who in this world know C# and not understand C????
I think you are missing a point here. Please reread the question. I would say that OP found a typo (<= -> <-) which lead to a bug in the code and is wondering why compiler has not helped to prevent it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.