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


Why do IF statements sometimes use { } and others not?
Updated: Views / Replies:434 / 5
Created: by Jayswiss Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • 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.

-- Big Mike, Site Administrator

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: 425 given, 1,125 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, California Republic
 
Futures Experience: Intermediate
Platform: Sierra Chart
Favorite Futures: ES, Mongolian Horse Futures
 
srgtroy's Avatar
 
Posts: 1,448 since Jan 2011
Thanks: 1,005 given, 2,205 received

@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)
Elite Member
Sarasota FL
 
Futures Experience: Intermediate
Platform: NinjaTrader, Sierra Chart
Favorite Futures: ES
 
Posts: 3,605 since Jan 2013
Thanks: 26,552 given, 11,008 received


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)

FuturesTrader71: TBA

Elite only

Al Brooks: TBA

Elite only

Jigsaw Trading: TBA

Oct 19

RandBots: TBA

Oct 24
     

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 05:58 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-09-23 in 0.10 seconds with 19 queries on phoenix via your IP 54.224.202.184