Why do IF statements sometimes use { } and others not? - NinjaTrader Programming | futures.io
futures io futures trading


Why do IF statements sometimes use { } and others not?
Started: by Jayswiss Views / Replies:304 / 5
Last Reply: Attachments:0

Welcome to futures io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.


You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike

Reply
 
Thread Tools Search this Thread
 

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

  #1 (permalink)
Elite Member
San Francisco, CA / USA
 
Futures Experience: Intermediate
Platform: NinjaTrader, TradingView
Favorite Futures: ES, TF, NQ, CL, GC
 
Posts: 29 since Jun 2016
Thanks: 4 given, 3 received

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.

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Cedar Rapids, iowa
 
Futures Experience: Intermediate
Platform: Ninjatrader
Broker/Data: Ninjatrader - Continuum
Favorite Futures: 6E, TF, 6J
 
Tasker_182's Avatar
 
Posts: 626 since Aug 2009
Thanks: 424 given, 1,121 received


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.

"Money is better than poverty, if only for financial reasons." - Woody Allen
Reply With Quote
The following 2 users say Thank You to Tasker_182 for this post:
 
  #4 (permalink)
Elite Member
San Francisco, CA / USA
 
Futures Experience: Intermediate
Platform: NinjaTrader, TradingView
Favorite Futures: ES, TF, NQ, CL, GC
 
Posts: 29 since Jun 2016
Thanks: 4 given, 3 received

Great explanation, thank you.

Reply With Quote
 
  #5 (permalink)
Elite Member
Los Angeles
 
Futures Experience: Intermediate
Platform: NinjaTrader/Sierra
Favorite Futures: ES
 
srgtroy's Avatar
 
Posts: 1,413 since Jan 2011
Thanks: 990 given, 2,130 received

Futures Edge on FIO
@Jayswiss

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

Reply With Quote
The following 2 users say Thank You to srgtroy for this post:
 
  #6 (permalink)
Market Wizard
Sarasota FL
 
Futures Experience: Intermediate
Platform: NinjaTrader, Sierra Chart
Favorite Futures: ES, CL
 
Posts: 3,601 since Jan 2013
Thanks: 26,251 given, 10,963 received
Forum Reputation: Legendary


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.


Last edited by bobwest; July 18th, 2016 at 09:14 AM.
Reply With Quote

Reply



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

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)
 

Mastering Trading Pysychology w/Brett Steenbarger & Trading Technologies

Elite only

PTMC Protrader Platform First Look w/Sergey Sokolov

Elite only

Spring Grains Outlook w/Sean Lusk @ Walsh Trading

Elite only

FIO Video Journal Challenge featuring NinjaTrader ($2,000+ of prizes)

April

Process above all else w/Anthony Crudele @ Futures Radio Show

Elite only

Machine Learning - Quantitative Trading w/Martin Froehler @ Quantiacs

Elite only

Market Dynamics w/Peter Davies @ Jigsaw Trading

Elite only

Ask Me Anything w/Patrick Rooney @ Trading Technologies

Apr 18

Ask Me Anything w/FuturesTrader71

Apr 19

Machine Learning w/Kris Longmore

Elite only

Market Analysis w/Dave Forss

Apr 25

Introducing iSystems with Stage 5 Trading

Apr 27
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing FOMC Statements on web FesxEurex Emini Index Futures Trading 2 November 20th, 2013 07:59 AM
C# inline 'if' with multiple statements Big Mike NinjaTrader Programming 5 August 9th, 2012 03:05 PM
Broker statements...safe to publish? patbateman The Elite Circle 11 August 1st, 2012 01:20 AM
Conditional Nested Boolean Statements serac NinjaTrader Programming 4 November 27th, 2011 06:44 PM
IB: Analyse Statements Henrik Beginners and Introductions 4 March 12th, 2010 06:29 PM


All times are GMT -4. The time now is 08:32 AM.

no new posts
Page generated 2017-03-24 in 0.09 seconds with 19 queries on phoenix via your IP 54.204.252.37