Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts

    exiting nested for loops

    If I've got the following code for checking whether a number exists within a 3x3 grid:
    Code:
    for rowCount=0 to 2
    	for colCount=0 to 2
    		if aWhichGrid(rowCount,colCount) & "" <> "" then
    			if cInt(aWhichGrid(rowCount,colCount)) = cInt(theNumber) then
    				rv = false
    				exit for
    			end if
    		end if
    	next
    next
    The "exit for" that's there only exits the innermost loop. I want to exit both; the number exists and I don't want to do any more looping. But I can't quite get my head round where to put the second "exit for"?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I think there's no counterpart in vbscript for javascript's label. In javascript, you can break out of the outermost loop by specifying a label.
    Code:
    outer:
    for (var i=0;i<5;i++) {
       for (var j=0;j<5;j++) {
          alert(i+","+j);
          break outer;
       }
    }
    The quick workaround I can think of in your case is to set the iterator variable used in the outermost loop (rowCount) to a number outside the range.
    Code:
    for rowCount=0 to 2
    	for colCount=0 to 2
    		if aWhichGrid(rowCount,colCount) & "" <> "" then
    			if cInt(aWhichGrid(rowCount,colCount)) = cInt(theNumber) then
    				rv = false
    				rowCount=3
    				exit for
    			end if
    		end if
    	next
    next
    But if you will use the value of rowCount in the succeeding codes, then you can try this another solution:
    Code:
    for rowCount=0 to 2
    	for colCount=0 to 2
    		if aWhichGrid(rowCount,colCount) & "" <> "" then
    			if cInt(aWhichGrid(rowCount,colCount)) = cInt(theNumber) then
    				rv = false
    				exit for
    			end if
    		end if
    	next
    	if rv = false then exit for
    next

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not:

    Code:
    for rowCount=0 to 2
    	for colCount=0 to 2
    		if aWhichGrid(rowCount,colCount) & "" <> "" then
    			if cInt(aWhichGrid(rowCount,colCount)) = cInt(theNumber) then
    				rv = false
    				colCount = 2
                                    rowCount = 2
    			end if
    		end if
    	next
    next
    Is this "grid" actually an array?


    If it's an array, you can...

    Code:
    arrString = JOIN(aWhichGrid,";")
    If instr(1,arrString, ";" & theNumber & ";") > 0 then
    	rv = false
    end if


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •