?
Solved

when are these needed {}

Posted on 2012-12-20
1
Medium Priority
?
200 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

741 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