-4

In Coldfusion, how do I increase a variable number by 1 for 5 loops?

I tried the following:

<cfset num = 19001>

<cfoutput>

<cfloop index="i" from="#num#" to="5">
    #num#
</cfloop>

</cfoutput>

But this is not working.

Gosi
  • 2,004
  • 3
  • 24
  • 36
  • If you look at the values and think about what the loop is being asked to do, you will understand why it does not work. The code says says start the loop at 19001 and stop when that number equals 5. If you are adding +1 to 19001 on each loop, obviously that value can never be equal to 5 ... – Leigh Feb 13 '17 at 14:42
  • I am new to coldfusion and trying to understand and your explanation made me understand, thank you Leigh. – Gosi Feb 14 '17 at 01:24

2 Answers2

5

You can do it like this:

<cfset num = 19001>

<cfoutput>
  <cfloop index="i" from="#num#" to="#num+5#">
    #i#
  </cfloop>
</cfoutput>
kayess
  • 3,384
  • 9
  • 28
  • 45
Simon Bingham
  • 1,205
  • 11
  • 15
4

You could simply loop from 1 to 5 and add 1 to your base number each time. Then your starting number can be anything and you don't need to calculate your end value ahead of time.

<cfset num = 19001>

<cfoutput>

<cfloop index="i" from="1" to="5">
    <cfset num = num + 1>
    #num#
</cfloop>

</cfoutput>
ale
  • 6,369
  • 7
  • 55
  • 65
William Greenly
  • 3,914
  • 20
  • 18