Solved

when are these needed {}

Posted on 2012-12-20
1
195 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
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but it's slow 28 70
Best resource to learn entity framework 2 33
C# Desktop Application 3 35
insert value of checklistbox checked 4 27
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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