Solved

when are these needed {}

Posted on 2012-12-20
1
197 Views
Last Modified: 2012-12-20
if (Project.ProjectID == 0) PID = 1; else PID = 2;

Why does this not have to have the brackets. When are they optional

if (Project.ProjectID == 0)
{
PID = 1;
}
else
{
 PID = 2;
}
0
Comment
Question by:Eamon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38710073
Brackets are optional if the body of your if (or else if, or else) consists of only one line. In your example, they would be optional, meaning this is exactly equivalent:

if (Project.ProjectID == 0) 
    PID = 1;
else 
    PID = 2;

Open in new window


The problem with using this approach is that it is easy to overlook the fact that this is only valid for single-ling bodies. For example, this would be a compilation error:

if (Project.ProjectID == 0) 
    PID = 1;
    PID = 2;
else 
    PID = 2;

Open in new window


The reason this fails is because the PID = 1; is the body of the if. Then you have a separate line of code in PID = 2;. Then you have an else that does not have an associated if (because of the intermediate PID = 2; line.

In your first example, this is actually very difficult to read. It would be easy for someone to completely overlook the else if they were scanning over your code.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question