2 Replies Latest reply on Dec 1, 2006 5:11 AM by Newsgroup_User

    Checking in a loop

    Level 7
      What is the proper method the do 'this' while executing a loop.

      Lets say you want to generate a random unique number.
      First you create a random number and when it is created you want to check if
      it is unique by checking it against the database.
      If it isn't, loop again and try again.
      If it is, then move to the next step of the code block.

      What I can't figure out is how break out of the loop if the check is
      correct.

      Here is some of my code. How do I break out of the loop if the recordcount
      of the 'checking' query EQ 0?

      What I use to generate a random number:
      <cfset ststring=structNew()>
      <cfloop index="i" from="1" to="4" step="1">
      <cfset a = randrange(0,9)>
      <cfset ststring["#i#"]=#chr(a)#>
      </cfloop>
      <cfset exid ="#ststring[1]##ststring[2]##ststring[3]##ststring[4]#">

      What I am going to use to check the number:
      <cfquery name="checknumber" datasource="**">
      SELECT * WHERE examid = #exid#
      </cfquery>
      <cfif checknumber.recordcount EQ 0> or <cfif checknumber.recordcount NEQ 0>