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:400 / 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: 424 given, 1,123 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,446 since Jan 2011
Thanks: 1,005 given, 2,199 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)
Market Wizard
Sarasota FL
 
Futures Experience: Intermediate
Platform: NinjaTrader, Sierra Chart
Favorite Futures: ES
 
Posts: 3,604 since Jan 2013
Thanks: 26,463 given, 10,999 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)

July Journal Challenge w/$1100 in prizes from TopstepTrader

July
 

An Afternoon with FIO member Softsoap

Elite only

Battlestations: Show us your trading desks and win $750 in prizes

August
 

Extended Ask Me Anything w/Brett Steenbarger

Elite only

Prototyping Python Strategies (part 3) w/Ran Aroussi

Elite only

Brannigan Barrett w/Axia Futures

Elite only

Introduction to Phillip Capital futures brokerage services

Aug TBD

How Monte Carlo Analysis Can Help Your Trading w/Kevin Davey

Elite only
     

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 10:44 AM.

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-07-21 in 0.09 seconds with 19 queries on phoenix via your IP 54.156.60.28