Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a global variable called @Count. I also have a for loop that increases the @Count by 1 each time I cycle through the loop. Is there a way for me to detect with AMPScript whether @Count is an odd or even number?

%%[

/* SET GLOBAL COUNT VARIABLE */
SET @Count = 0

/* INCREASE THE COUNT BY 1 EACH TIME */
FOR @i = 1 TO @increaseCount DO
  SET @Count = Add(@Count,1)
NEXT @i

/* DO SOMETHING IF COUNT IS EVEN OR ODD NUMBER */
IF @Count == "even" THEN
  BLANK
ELSEIF  @Count == "odd" THEN
  BLANK
ENDIF

]%%
share|improve this question
up vote 6 down vote accepted

I'd suggest using the MOD() AMPScript function. If @sum is evenly divisible by 2 then it's even, otherwise it's odd:

%%[

var @i, @sum, @max
set @sum = 0
set @max = 10

FOR @i = 1 TO @max DO
  SET @sum = Add(@sum,1)
NEXT @i

IF mod(@sum,2) == 0 THEN
  outputline(concat("<br>", @sum, " is even"))
ELSE
  outputline(concat("<br>", @sum, " is odd"))
ENDIF

]%%
share|improve this answer
1  
Haha guess I was right. Hadn't used AmpScript before but figured this would be the approach. – Adrian Larson Feb 29 at 20:04
    
Awesome! Thanks for the support you guys. This method works great. – Nolan Feb 29 at 21:23

Try using MOD(@Count, 2) == 1. Here is the documentation for the MOD function. If it's 0, you're even. If it's 1, you're odd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.