IIf function versus if statement

As mentioned by @fxshrat in ValueWhen - how to use a typical mistake users make is that they mix up if statement with IIf() function.

IIf() (which stands for "immediate if") is a function, which means it takes arguments and returns the result

result = IIf( condition, YesValue, NoValue );

It works with both numbers (scalars) and arrays. When any of arguments is an array it returns array. It goes bar by bar, checks condition again bar by bar and returns either corresponding array value from YesValue or NoValue arrays depending if condition at given bar is true or not.

Note that IIf() function does NOT change the flow of the program. It evaluates ALL arguments, both yesValue and noValue.

On the other hand if / if-else is a flow-control statement. It is used to change the flow of the program. It decides whenever take this or that path in the program. It decides once and requires scalar value (either true (non-zero) or false (zero)) to decide.

You can not use if / if-else on arrays directly because arrays have many values - different value in each array element.

The only way to use if / if-else statements on arrays is to pick up array element you want to use for flow control, for example:

if( array[ BarCount - 1 ] > 0 )
  // do something else when last element of array 
  // is greater than zero

An interesting example is showing what IIf() function does by re-implementing it using if-else statement.
In the code below we are using for loop to go thru all bars and if-else statement checks individual array elements one by one to decide which path to take.

function ImmediateIf( conditionArray, yesValueArray, noValueArray )
     for( bar = 0; bar < BarCount; bar++ )
          if( conditionArray[ bar ] ) 
              result[ bar ] = yesValueArray[ bar ];
              result[ bar ] = noValueArray[ bar ];
     return result;
Whats the difference between these iif and if?
How to write If current Signal is Buy
Problem with IF funcion in Fibonacci AFL
Duplicate: Call procedure selectively based on array value
How to run the AFL between time intervals
Generating value using if else statement
Mistake: Buy/Sell not working in If loop
Conditional IIf in rsi bars
How to Exit a 'For...' loop that contains global .afl instructions?
IIF or IF ? Stopping conditions from executing signals
How does "if" work? (shortcut evaluation)
Plot text : show percentage of Daily Open-Close compared to avg. Also display "Large" when > 85%
Does the iif() function runs faster than if .. else block?
Recreating code from "Weekend trend trader" book
How to plot VWAP indicator value on every candle?
Sell Price and If conditional
Multiple values for TakeProfit Stop
How to take profit on Heiken Ashi bars
How to use the bars of today for looping
Help logica for amibroker
Need help in Trailing Stop Loss
How to get a Position Price at each moment?
Black & Scholes
Conditional Plot() something or nothing
AFL Code not giving desired BUY and Sell Signal
Need Volume Breakout Pop-up AFL Code
Incorrect use of IIf, was: Dividing by 0 yielding warning and unexpected results even though this situation should never happen
Need help in generating a score on bullish indicator
Daily RSI cloud color change based on MTF RSI level
Problem with SUM function and variable periods
John F. Ehlers Rocket RSI
I am finding below attached error. How to solve it
Duplicate: Call procedure selectively based on array value
Duplicate: Call procedure selectively based on array value
Change Position size based on Volume
Using IF in FILTER
Not using LastValue in Backtest
Adjusting Positionsizes while always remaining in the market
Estimate transaction volumn
Question About the AFL
Does the iif() function runs faster than if .. else block?
BarColor  = IIf(nextBarHigher,colorGreen,IIf(nextBarLower,colorRed,colorBlue));


BarColor  = IIf(nextBarHigher,colorGreen,colorRed)

Need some clarification on the differences.

When would colorBlue appear ?
What difference should I be expecting ?

I was reading and trying to understand this.



IIF is for arrays, if is not. In other words, IIF evaluates the condition for every bar, where as if evaluates a single result condition.

You can read this page for example, specifically the IIF and if statement parts : https://www.amibroker.com/guide/a_mistakes.html

1 Like

Right i got it now, i got confused, by iff and if



IF - Programming Flow Control statement.

IIF - ImmediateIF = Conditional return

Your first line would work. If the next bar is higher, you get Green.
If next bar lower you get Red. If Neither is true you get Blue.

Your second line will not run.

Think of IF as a Traffic Sign. If (True) turn right (use code), ELSE
turn Left (use different code).

Hope this helps.



Hi Folks,

I must be doing something wrong but this is triggering the shell execute on every bar

IIf(Buy == True, ShellExecute("C:\\Users\\Name\\.spyder2-py3\\temp.py", "", "" ),0);

Can you please advise?


See already existing topic

Your code is incorrect. For conditional execution / flow control you have to use if-else statement.

Correct code for conditional execution based on last buy signal:

if( LastValue( Buy ) ) 
   ShellExecute("C:\Users\Name\.spyder2-py3\temp.py", "", "" );

Read this thread from the beginning for more information.


Thank you Tomasz,

I noticed that in the Buy condition if I add the following condition

barcomplete = BarIndex() < LastValue(BarIndex());

the shellexecute stops working. Can you please help understand ?

Thanks again.

I wrote already: “Read this thread from the beginning” - ALL explanations are already provided in the very first post in this topic. Key to understanding is the TYPE of variable - whenever it is scalar or array.

Thanks Tomasz,

I thought barcomplete would return True or False which the IF statement can evaluate. Even when I give the barcomplete a LastValue treatment it doesn't seem to work. Sorry I am just having a hard time understanding arrays and how the IF works.

if( LastValue( Buy )  and LastValue( barcomplete )) 
   ShellExecute("C:\Users\Name\.spyder2-py3\temp.py", "", "" );

Moderator comment: the above code makes no sense. LastValue( barcomplete) is always false because the last bar by definition is not complete. You should just use
if( LastValue( Buy ) ) { ... }


you can add your variables to the watch window in debugging mode.
I think that will help you to see which variables are arrays or numbers and the results of your expressions.

barcomplete=BarIndex() < LastValue(BarIndex());