Lesson Seven, Part 7, Accumulating Text in a Loop

So far we have been using math to keep adding values (ie totalCost = TotalCost + Cost).  We can also store text values the same way.  What we are going to do is create a program that will keep adding to text that is stored in a place in memory.  We will create a loop and every time through it will add text to a place in memory that already has some text stored in it.  We'll see that it works the same as it did when we accumulated numbers.

We have been asked to write a program that allows a person to enter the year that they graduated high school and how many reunions they would like to see in the future.  For example if the user says that they graduated in 1996 and wanted to see when the next five reunions would be.  This is the resulting output:

This is the Schedule of reunions starting after your graduation in 1996:

First reunion is in 2006
Reunion number 2 will be in the year 2016
Reunion number 3 will be in the year 2026
Reunion number 4 will be in the year 2036
Reunion number 5 will be in the year 2046

Assumptions:
Only years will be used not days, months, etc

Given:
The Year Graduated
The number of reunions required

Calculations:
Number of reunions + 1
Year Graduated + 10

Output:
"This is the Schedule of reunions starting after your graduation in" & The Year Graduated
"First reunion is in" The Year Graduated + 10
"Reunion number" & Number of reunions & "will be in the year" & The Year Graduated

This is the flowchart:

Now let's write the code:

<Script language ="vbscript">
Option Explicit
DIM YearGraduated,Howmanyreunions,gradyearaccumulator, numofloops, Reunion

Howmanyreunions = inputbox("How many graduation reunions do you want to see")
YearGraduated = inputbox("What year did you graduate HS?")
numofloops = 1
Do while numofloops < (howmanyreunions + 1)

        If gradyearaccumulator = 0 then
            gradyearaccumulator = yeargraduated + 10
            Reunion = "First reunion is in " & gradyearaccumulator
        Else
            Reunion = Reunion & "<br>Reunion number " & numofloops & " will be in the year " & gradyearaccumulator
        End If
    gradyearaccumulator = gradyearaccumulator + 10
    numofloops = numofloops + 1
Loop
Document.write "This is the Schedule of reunions starting after your graduation in " & Yeargraduated & ": <P>" & Reunion
</Script>