NexusFi: Find Your Edge


Home Menu

 





Why do IF statements sometimes use { } and others not?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Jayswiss with 2 posts (0 thanks)
    2. looks_two Quick Summary with 1 posts (0 thanks)
    3. looks_3 bobwest with 1 posts (0 thanks)
    4. looks_4 Tasker_182 with 1 posts (2 thanks)
    1. trending_up 1,613 views
    2. thumb_up 4 thanks given
    3. group 3 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

Why do IF statements sometimes use { } and others not?

  #1 (permalink)
 Jayswiss 
San Francisco, CA / USA
 
Experience: Intermediate
Platform: NinjaTrader, TradingView
Trading: ES, TF, NQ, CL, GC
Posts: 29 since Jun 2016
Thanks Given: 4
Thanks Received: 3

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.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
How to apply profiles
Traders Hideout
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
21 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 
Tasker_182's Avatar
 Tasker_182 
Cedar Rapids, iowa
Legendary Market Wizard
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Ninjatrader - Continuum
Posts: 716 since Aug 2009
Thanks Given: 476
Thanks Received: 1,401


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;}

You might look at msdn for coding style guidlines: https://msdn.microsoft.com/en-us/library/ff926074.aspx

You might try a search on the internet for C# questions as you will find lots of reference materials.

Hope this has helped.

Be yourself; everyone else is already taken. Oscar Wilde
Reply With Quote
Thanked by:
  #4 (permalink)
 Jayswiss 
San Francisco, CA / USA
 
Experience: Intermediate
Platform: NinjaTrader, TradingView
Trading: ES, TF, NQ, CL, GC
Posts: 29 since Jun 2016
Thanks Given: 4
Thanks Received: 3

Great explanation, thank you.

Started this thread Reply With Quote
  #5 (permalink)
 
srgtroy's Avatar
 srgtroy 
Los Angeles, California Republic
Legendary  R.I.P. 1965-2023 
 
Experience: None
Platform: Sierra Chart
Broker: CQG
Trading: ES
Posts: 1,928 since Jan 2011
Thanks Given: 1,375
Thanks Received: 3,722

@Jayswiss

Save yourself future debugging problems and just always use the brackets, even if its only one line of action.

Reply With Quote
Thanked by:
  #6 (permalink)
 
bobwest's Avatar
 bobwest 
Western Florida
Site Moderator
 
Experience: Advanced
Platform: Sierra Chart
Trading: ES, YM
Frequency: Several times daily
Duration: Minutes
Posts: 8,168 since Jan 2013
Thanks Given: 57,452
Thanks Received: 26,278


srgtroy View Post
@Jayswiss

Save yourself future debugging problems and just always use the brackets, even if its only one line of action.

Correct.

Otherwise, you will find yourself doing this:

First version of a piece of code:

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.

Bob.

Reply With Quote




Last Updated on July 18, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts