Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Why do IF statements sometimes use { } and others not?
I'm trying to understand how IF statements work. In some examples from Ninjatrader, I will see if statements used without {then} brackets. But without them, I guess I don't understand how it would work.
Example:
if (condition_a)
variable = 123;
if(condition_b) {
variable = 555;
}
if(condition_c) {
variable= 444;
if(condition_c2)
variable=445;
}
I'm confused. Especially about nesting the IF's and when when/not to use the {} brackets.
Can you help answer these questions from other members on NexusFi?
In C# when the condition of the if statement is true, the statement immediately after will be executed. If not true the statement immediately after is skipped.
In the case of if(condition) action; that is all that happens. So it can be written as shown or as you have seen as if (condition) {action}; Either one is correct and will work.
The "{}" are used to encapsulate the action as there can be more than one action statement, for example:
if (Condition)
{
action 1;
action 2;
action 3;
etc;
}
In the above example when the condition is true all of the action statements would be executed.
What is confusing is that you have seen it written so many ways but the main view is if the condition is true only one statement (or statements if encapsulated in {}) after will be executed.
Some people from a coding style perspective will use {} even if only one action statement is contained and they would do this to be consistent in their code style or perhaps readability as when you see { } you would know that all things between them are related by a condition statement.
Here is a link to the Ninjatrader7 helpguide educational section: NinjaTrader Version 7 which seems to advise if (condition {action;}
if (condition)
doThis; // This is completely valid, and executes doThis only if the condition is true. It skips it if condition is false.
(Or, you could put doThis on the same line -- "white space", including line breaks, don't matter.)
But then you want to add something that will also execute based on condition, but you are hasty and you write it like this:
if (condition)
doThis;
alsoDoAnotherThing;
// This will execute doThis if the condition is true, AND IT WILL ALWAYS EXECUTE alsoDoAnotherThing, whether condition is true or false, because it's the next statement after doThis.
But putting them in brackets will make both statements a block, as @Tasker_182 said, and the code will execute or skip the entire block, based on whether condition is true or not:
if (condition)
{
doThis;
alsoDoAnotherThing;
}
There is nothing wrong with not using the brackets if you have only one thing to execute, but you open yourself up to simple human errors that are hard to debug afterwards.