Why does C # require parens around conditional expressions?

I just read the SO question in Python and noticed the absence of parentheses in the for-loop. It looked good to me, then I thought: why does C # require them?

For example, I now need to write:

if (thing == stuff) { } 

and

 foreach (var beyonce in allthesingleladies) { } 

So I wonder why I can not write:

 if thing == stuff { } 

Is there any syntactic ambiguity in this statement that I don't know about?

PS, funny, braces can be optional for single-line:

 if (thing == stuff) dostuff(); 
+6
syntax c #
source share
6 answers

The system needs to know when the condition ends and the instruction begins. This means that either the parens around the condition are optional, or the brackets around the instruction are optional. C # designers decided to make curly braces optional (in case there is one operator).

Consider this:

 if x == 12 - x == 3 ? x++ : x--; 

You can write a parser that can determine where the condition ends and the instruction begins, but requiring parentheses greatly simplifies it. In the case of my example, the grammar indicates that the complete if (x == 12) { -x == 3 ? x++ : x--; } if (x == 12) { -x == 3 ? x++ : x--; } if (x == 12) { -x == 3 ? x++ : x--; } . However, you do not know what - is the beginning of the instruction until you press the second == , which already contains 3 tokens.

Since the proper analysis of such examples requires viewing an arbitrary number of tokens in front of the input stream (and perhaps this makes the code more difficult for people), there is a good reason for the requirement for partners.

+8
source share

This is a syntax function that returns to C, or maybe to some language before.

Essentially it was a choice between

 if (condition) statement; 

and

 if condition then statement; 

At the time C was conceived, shorter notation and fewer keywords were a trend.

+4
source share

[I know that I will probably cry for it, but I have to say it anyway]

As a predominantly vb programmer, there is one thing that annoys me to the bone. In my opinion, parens should be optional, and bindings are required. If this is too much to ask, then Microsoft can โ€œborrowโ€ from vb to C # by introducing the equivalent of C # vb Then.

But why bother then? Microsoft has developed C # based on C, C ++, and Java, all of which set the if condition in parens, so why will C # be different?


Edit

Honestly, I think that based on C # 4 "accepting optional and named parameters", a function that has always been in VB (at least with VB6), the next version of C # (C # 5.0) introduce a "new" function , so you do not need to enter unnecessary parentheses.

So how is the triple if condition ? truePart : falsePart if condition ? truePart : falsePart having if condition then something or for those who are not in C # similar to vb, if condition do something would not be a bad idea.

+2
source share

This is the choice of language design that appeared in Java, which, in turn, was from C ++, which, in turn, was from C, which, in turn, was from

Python does not need handcuffs, since indentation indicates intent. F # does the same. This is a good idea (IMO).

0
source share

By the way, if I had my drummers, I would allow one operator outside of the parens: a leading exclamation point. It annoys me that code testing for complex negative conditions ends with an extra set of parentheses compared to code testing for complex positive ones.

0
source share

Simply put, this is due to the way C / C ++ / C # handles empty space. You need some sort of delimiter, since statements can span multiple lines. You also need a way to โ€œblockโ€ or group statements together into a block.

  • C / C ++ / C # use (), {} 's
  • Using VB / VB.NET / VBScript / VBA Function / End Function, Sub / End Sub, If / Then / Else / Else If / End If and so on. (Only for blocking, as VB-based languages โ€‹โ€‹do not support multi-line statements.)

If your specific question, you use () to create an expression. Therefore, you need to specify that the compiler execute this expression, since {} (blocking delimiters) are not required.

0
source share

All Articles