[SalesForce] For loop within for loop

I have a custom object record with a start date, an end date, and a amount field. I want to be able to take the difference between the two dates in number of months, divide the amount by the difference and then create related records based on the difference count. To make a basic schedule.

I think I have to do two for loops in my class, however, I've never done two at the same time before so I'm unsure how to get started.

I know that to get the total number of months I can do this:

Integer numOfMonths = start_date.monthsbetween(end_date) 

I know that to create the records I can do something like this:

for(Integer i = 0; i < numOfMonths; i++) {
    insert new Revenue_Schedules__c(
        Amount__c = amount/numOfMonths,
        Date__c = start_date.addmonths(numOfMonths), 
        );      

How do you loop through values in the newMap to get each variable and then loop again to get records to insert?

-=EDIT=-

input example:
Revenue record with start_date = 10-1-2016, end_date = 9-1-2017, amount = 1100

output expectation:
12 Revenue Schedule records
* record 1, date = 10-1-2016 amount = 100
* record 2, date = 11-1-2016 amount = 100
* record 3, date = 12-1-2016 amount = 100
ect..

Best Answer

You should never do DML statements (such as insert) inside loops, this will easily get you off limits. Create a List outside the loop, iterate over your trigger in the first loop, create new records and add them to the list in the second loop, then insert everything outside the loop. Something like this:

List<Revenue_Schedule__c> new_records = new List<Revenue_Schedule__c>();

for (Your_Trigger_sObject__c current : trigger.new) {
    Integer numOfMonths = current.start_date.monthsbetween(current.end_date);
    for (Integer i=0; i < numOfMonths; i++) {
        new_records.add(
            new Revenue_Schedules__c(
                Amount__c = current.amount / numOfMonths,
                Date__c = current.start_date.addmonths(numOfMonths), 
            )
        );
    }
}

insert new_records;

The important is to keep the DML operation outside the loop. The same applies to queries, keep them always out.