Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:36:23+00:00 2026-05-16T07:36:23+00:00

I was just reading an SO question on Python, and noticed the lack of

  • 0

I was just reading an SO question on Python, and noticed the lack of parentheses in a for-loop. Looked nice to me, then I wondered: why does C# require them?

For example, I currently need to write:

if (thing == stuff) {
}

and

foreach (var beyonce in allthesingleladies) {
}

So I am wondering why I can’t write:

if thing == stuff {
}

Is there a syntactic ambiguity in that statement that I am unaware of?

PS, funnily, braces can be optional for one-liners:

if (thing == stuff)
  dostuff();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:36:24+00:00Added an answer on May 16, 2026 at 7:36 am

    The system needs some way to know when the condition stops and the statement starts. That means that either parens around the condition are optional, or the braces around the statement are optional. The designers of C# chose to make the braces optional (in the event that there’s a single statement).

    Consider this:

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

    A parser could be written that can tell where the condition ends and the statement starts, but requiring the parentheses makes it substantially easier. In the case of my example, the grammar indicates that the full statement is if (x == 12) { -x == 3 ? x++ : x--; }. However, you don’t know that the - is the beginning of the statement until you hit the second ==, which is already 3 tokens into it.

    Since correctly parsing such examples requires looking an arbitrary number of tokens ahead in the input stream (and it arguably makes the code harder for humans to read), there’s a good reason for requiring the parens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just reading a recent question on using conditionals in Linq and it
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
Just finished reading Jon Skeet's article about events and delegates and got a question.
I just finished reading the HTML5 Developer's Cookbook and have a question. I've read
Forgive if this question is silly. I just started reading the SugarCRM developer documentation
This is just a simple question. I've been reading the source of something which
I feel rather foolish as this is just a short question. I was reading
I was just reading through Learning Python by Mark Lutz and came across this
I was just reading a question about how to add parameters to a SqlCommand
I have just started reading learn python the hard way and I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.