How to loop with condition

Hi, I would like to create a list of dates 1 month apart, from a date in the past until the last date before now. There is no while loop to check a condition to determine if to carry on with the next iteration. Is there a suggestion on how to do so?
Here is some code that does it once at least

var firstDate = events.get(0).dateTime;
var nextDate = firstDate.plus(months(1));
var dates = listOf<DateTime>();
if (nextDate.isBefore(now(), false)) {
    dates.with(nextDate);
};

This may not be exactly what you are looking for, but you could use

  • A recursive function Recursion - Noumena Documentation (be mindful of stack overflow risk).
  • Duration between the two dates, compute a number of months from the conversation to seconds (unclear robustness), then use range, map plus(month(.)) onto range. Can surely be made robust with conservative calculation of number of months, filter against now().