Operators for chaining conditions: "+" or "||"

The operators used to chain conditions in the following snippet appear to be equivalent when checking the values of “chain1” and “chain2” in the debugger and watch window:

cond1 = False;
cond2 = True;
cond3 = False;
cond4 = False;
cond5 = False;

chain1 = cond1 + cond2 + cond3 + cond4 + cond5;
chain2 = cond1 || cond2 || cond3 || cond4 || cond5;

if(chain1) printf("chain1ok"+"\n");
if(chain2) printf("chain2ok"+"\n");


Are there any cases in which these 2 operators would not produce equivalent results when used for chaining conditions?

They are not equivalent.

|| operator is a boolean (logical) OR operator. It will give you only True (1) or False (0) result. You will get just 1 (True) if ANY of chained conditions is met.

+ operator is arithmetic addition. It will give you 0, 1, 2, 3, 4, 5 depending on how many chained conditions are met.

printf("%g", chain1 ); // this will print 0, 1, 2, 3, 4 or 5
printf("%g", chain2 ); // this will print 0 or 1

Use debugger or _TRACE to debug / understand the code.

1 Like

Thank you very much Tomasz!!

But then, what is the difference between “OR” and “||” - same with “AND” and “&&”?

OR is a synonym to ||
AND is a synonym to &&

These are provided because words are simpler for beginners, while && and || are second nature to C programmers.

1 Like

And there are also bitwise operators --> A|B and A&B in AFL for example:

SetChartOptions( 0, chartDisableTooltips | chartDisableYAxisCursor | chartHideQuoteMarker, chartGrid0, 0, 0, 0 );


// the example below is incorrect
// Anything inside quotation marks IS NOT operator. 
// "No|Yes" is just a text, nothing more
ParamToggle( "Text Highlight", "No|Yes", 1 );  // NOT AN OPERATOR!


… but when in general (apart from the cases similar to those above) these bitwise operators should be used in AFL?

And quite frankly, there is something I don’t quite understand (I didn’t think about it before). In the first example above, using “I” operator we tell AmiBroker to apply all the mentioned chart options (as if we used And), but in the second example we tell to choose “Yes” or “No”…

Moderator comment: second example was commented because it is incorrect

Your second example is incorrect. In the second example | is used INSIDE the string and it is NOT an operator. Everything inside double quotation marks is JUST A TEXT. ParamToggle uses | character as SEPARATOR between options. It could use comma or semicolon but then this would prohibit using commas or semicolons in option texts, so | is used as separator as nobody uses that in normal text.

Hint: sometimes LESS is more. If somebody asks for boolean (logical) operators it is better NOT to mention bitwise operators because it would just create additional confusion. Some things are better left untold unless somebody specifically asks.