Nested if (SomeCondition) blocks

The forum for experienced coders to upload their helpful hints, tips and lessons.
Post Reply
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Nested if (SomeCondition) blocks

Post by SteveHopwood »

Lifesys has introduced me to the joys of nested if (SomeCondition) blocks. They are really cool. :lol:

From the gms' Double Big Mac V7 mod Steve EA:

Code: Select all

      if (curr == StringSubstr(pair[i],0,3))
         if (CheckIfOpen(pair[i]+postfix) == 0)
            if (CheckMACDH4(StringSubstr(pair[i],3,3)) == -1)
               if (!UseFilter_ADX || CheckADX(pair[i], buy))
                  if (SlopeTimeFrame == 0 || SlopeVal >= SlopeBuyOnlyLevel)
                     OpenBuy(pair[i]+postfix);
OpenBuy(pair+postfix); is only executed if all the previous conditionals pass. I have attached Paul's explanation.

:D
You do not have the required permissions to view the files attached to this post.
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: Nested if (SomeCondition) blocks

Post by mobthehop »

Thanks Steve & lifesys - wondered for the longest time how to do 2 or more conditions ( a la "ducks in a row") as basis for a trade decision

Cheers
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Nested if (SomeCondition) blocks

Post by dietcoke »

This is absolutely the way to code multiple conditions because if you put multiple conditions in a single if statement, Empty4 always tests ALL the conditions even if the first one fails.
garyfritz

Re: Nested if (SomeCondition) blocks

Post by garyfritz »

You can do this as a single "if" with a complex condition, e.g.:

Code: Select all

if ((curr == StringSubstr(pair[i],0,3))
     && (CheckIfOpen(pair[i]+postfix) == 0)
     && (CheckMACDH4(StringSubstr(pair[i],3,3)) == -1)
     && (!UseFilter_ADX || CheckADX(pair[i], buy))
     && (SlopeTimeFrame == 0 || SlopeVal >= SlopeBuyOnlyLevel)
     )
         OpenBuy(pair[i]+postfix);
...but as DC said, Empty4 always tests all of those conditions and you might not want to do that. The multiple if statements should (assuming a competent compiler, but remember this is Empty4 we're talking about) compile into code that's just as efficient as the "&&" code.

But minor little things like efficiency of the compiled code really aren't worth worrying about. A much bigger deal is the "lazy evaluation" DC's talking about. Where most of those tests are calling functions, each of the tests might be an expensive operation. The single-if code using && evaluates ALL of those functions, even if the very first test fails. The multiple-if code quits as soon as one of them fails. That might save you quite a bit.

One thing to remember: the "then" part of an if is a single "statement," whether that's one actual statement or a compound statement in {}'s. Each if is considered a single "statement," no matter how complex the logic is inside the "then" part, so in this construct you don't need {}'s around the "then" parts. That looks cleaner in this nested-if test. But be careful about omitting {}'s in code that you might add to later, like this:

Code: Select all

if (test)
  thenPart;

// then you add more to the "then":

if (test)
  thenPart;
  thenPart2;
You'll find that thenPart2 gets executed whether the test is true or not. Guess how I know. :oops:
Post Reply

Return to “Coding Lessons - info for all”