开发者

Understanding IF-statement

I kind of understand the basics of what it does, but it gets confusing. If anyone could r开发者_StackOverflow社区e-write it into simple pseudo code, it would be greatly appreciated!

I'm writing a PHP application and I need the same algorithm in PHP, but it's not working.

=IF(CC_stolpar=2;
    IF(Antal_slanor<4;
       6*Antal_dubbelgrind;
       Antal_dubbelgrind*Antal_slanor*2)
    +
    IF(Antal_slanor<4;
       3*Antal_enkelgrind;
       Antal_enkelgrind*Antal_slanor);
    0)


I'm not sure why he's testing for antal_slanor<4 twice, it should be equivalent to

=IF(CC_stolpar=2;
    IF(Antal_slanor<4;
       6*Antal_dubbelgrind              + 3*Antal_enkelgrind;
       Antal_dubbelgrind*Antal_slanor*2 + Antal_enkelgrind*Antal_slanor);
    0)


This has nested IF statements, which follow the pattern IF-THEN-ELSE

IF (condition); what to do if condition is true; what to do if condition is false

So your outer IF is IF(CC_stolpar=2 THEN (do all this complicated stuff) ELSE 0.

That's pretty simple. Either CC_stolpar is 2 or it isn't, and if it has any other value than 2, then the result is zero.

That "complicated stuff" is summing the results of two different IF statements.

The first one is

IF(Antal_slanor<4;
6*Antal_dubbelgrind;
Antal_dubbelgrind*Antal_slanor*2)

If Antal_slanor is less than 4, then multiply Antal_dubbelgrind times 6.

If Antal_slanor is greater than or equal to 4, then multiply Antal_dubbelgrind times Antal_slanor times 2.

You will only get one result from this, based on whether Antal_slanor is less than 4 or not.

You repeat essentially the same process for the other IF, and sum the results.

IF(Antal_slanor<4;
3*Antal_enkelgrind;
Antal_enkelgrind*Antal_slanor);

Both of these nested IF statements are evaluating whether Antal_slanor is less than 4. So, you could merge them into a single IF if that makes sense to you.

In the end, I would suggest for your own sanity and the sanity of others who may have to read this after you, keep the formula as simple as possible in your code.


You just have to look at the pieces rather than in a line.

As a hint, it CC_stolar=2, then the result is the sum of two nested IF statements.

Perhaps some brackets would help:

=IF(CC_stolpar=2;
    {
    IF(Antal_slanor<4;
        {
            6*Antal_dubbelgrind;
        } else {
            Antal_dubbelgrind*Antal_slanor*2)
        }
    +IF(Antal_slanor<4;
        {
            3*Antal_enkelgrind;
        } else {
            Antal_enkelgrind*Antal_slanor)
        }
    } else {
        ;0)
    }


IF CC_poles == 2 then 
    IF NUMBER_OF_POLES < 4 THEN
         RESULT = 6*NUMBER_OF_DOUBLE_GATES + 3* NUMBER_OF_SINGLE_GATES;
      ELSE
         RESULT= NUMBER_OF_DOUBLE_GATES*NUMBER_OF_POLES*2 + NUMBER_OF_SINGLE_GATES *
                 NUMBER_OF_POLES
    ENDIF
ELSE
    RESULT = 0
ENDIF
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜