cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
2024 Technology Preview Program

2024 Technology Preview Program:
Master powerful new features and shape the latest BIM-enabled innovations

GDL
About building parametric objects with GDL.

Goto inside For Next loop that jumps outside of the loop causes problem.

Anonymous
Not applicable
Hi, I would like to ask how I can rewrite the following code so I do not get a crossing loops error.
If I use endif I get error endif without if.
If I do not use it I get Next without For at label 4310
4280: FOR I=1 TO N
	  FOR J=1 TO FK
	  IF ABS(Q-EPS) >= EPS GOTO 4300 
	  NEXT J
!     ENDIF
4290: GOTO 4310	
4300: TEXT2 0,-9-N-NS-I,I
	  FOR H=1 TO FK
	  TEXT2 3*H,-9-N-NS-I,Q						
	  NEXT H                                                
4310: NEXT I
Thanks in advance.
13 REPLIES 13
A_ Smith
Expert
you shouldn't use GOTO, unless its really necessary (i'm new at gdl and there weren't a single time when i needed it).
I didn't have all code - with those q, n, ns, but i think it could be something like this
for i=1 to n
	for j=1 to fk
		if abs(q-EPS) >= EPS
			text2 0,-9-n-ns-i,i
		endif
	next j

	for h=1 to fk
		text2 3*h,-9-n-ns-i,q						
	next h                                                
next i
AC 22, 24 | Win 10
Anonymous
Not applicable
Hi A. Smith,

it seems you did not understand the loop.

if greater than 0 then run second inner loop H
if not then I (outer loop) loops and brings again the first inner loop to check again if greater than zero.
I have seen a lot of GDL code written in the style you have done it and it always confuses me as its hard to follow. You should be able to see the logic at a glance. A. Smith's code is much easier to read and understand.

a couple of little rules to help is always separate sub routines with a graphical demarcation,
don't use numbers to name your sub routines, instead use descriptions,
use tabs to see what lies in what.

I took your code and restructured it so I could see what was happening:

step 1: demarcation of subroutines.

!!----------------------------------------------
4280:

FOR I=1 TO N
	FOR J=1 TO FK
	IF ABS(Q-EPS) >= EPS GOTO 4300 
	NEXT J
!ENDIF
!!----------------------------------------------
4290:

GOTO 4310

!!----------------------------------------------
4300:

TEXT2 0,-9-N-NS-I,I
	FOR H=1 TO FK
	TEXT2 3*H,-9-N-NS-I,Q						
	NEXT H
                                                
!!----------------------------------------------
4310:

NEXT I
step 2: removal of unnecessary subroutines and correct tabbing

!!----------------------------------------------
4280:

FOR I=1 TO N
	FOR J=1 TO FK
		IF ABS(Q-EPS) >= EPS then
			TEXT2 0,-9-N-NS-I,I
			FOR H=1 TO FK
				TEXT2 3*H,-9-N-NS-I,Q						
			NEXT H
		endif
	NEXT J
NEXT I
!!----------------------------------------------
this shows the logic of your current code more easily and will help you understand what is happening.
if you want the "H" loop to be outside of the "J" loop then you simple move it outside to end up with what A. Smith wrote. Also give subroutines a name in inverted commas and "return" from it:

gosub "subroutine name"

end!!----------------------------------------------
"subroutine name":

FOR I=1 TO N
	FOR J=1 TO FK
		IF ABS(Q-EPS) >= EPS then
			TEXT2 0,-9-N-NS-I,I
		endif
	NEXT J
	FOR H=1 TO FK
		TEXT2 3*H,-9-N-NS-I,Q						
	NEXT H
NEXT I

return!!-------------------------------------------
Structuring your code so you are effectively "drawing" the logic will help you write more complex logic with ease. also don't overuse subroutines; as a general rule each subroutine should encompass a complete function, unless the function has many complex parts.
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists
Hi A. Smith,

it seems you did not understand the loop.

if greater than 0 then run second inner loop H
if not then I (outer loop) loops and brings again the first inner loop to check again if greater than zero.
based on this response, is this what you are after:

gosub "subroutine name"

end!!----------------------------------------------
"subroutine name":

FOR I=1 TO N
	FOR J=1 TO FK
		IF ABS(Q-EPS) >= EPS then
			TEXT2 0,-9-N-NS-I,I
		else
			FOR H=1 TO FK
				TEXT2 3*H,-9-N-NS-I,Q						
			NEXT H
		endif
	NEXT J
NEXT I

return!!-------------------------------------------
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists
But I think it is more this:

gosub "subroutine name"

end!!----------------------------------------------
"subroutine name":

FOR I=1 TO N
	FOR J=1 TO FK
		IF ABS(Q-EPS) >= EPS then
			TEXT2 0,-9-N-NS-I,I
		else
			TEXT2 3*J,-9-N-NS-I,Q						
		endif
	NEXT J
NEXT I

return!!-------------------------------------------
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists
what the above is saying is:

you have an array called "Q" that has "N" columns and "JK" rows.
you want to cycle through each value in the array column by column and
where there is a "0" value run "TEXT2 0,-9-N-NS-I,I"
otherwise run "TEXT2 3*J,-9-N-NS-I,Q"

your "IF ABS(Q-EPS) >= EPS then" is also wrong because if Q = 0 or > EPS*2 then it will return true.
perhaps you should write it like this "IF ABS(Q) > EPS then" because EPS is your buffer so anything less than it should be considered "0" to avoid issue. though if you change it to this you then have to swap the two text results to achieve your old "H" loop if > 0, or use < like this "ABS(Q) < EPS then"

So is this what you want:

gosub "subroutine name"

end!!----------------------------------------------
"subroutine name":

FOR I=1 TO N
	FOR J=1 TO FK
		IF ABS(Q) < EPS then
			TEXT2 0,-9-N-NS-I,I
		else
			TEXT2 3*J,-9-N-NS-I,Q						
		endif
	NEXT J
NEXT I

return!!-------------------------------------------
Creator of Cadswift's parametric GDL libraries
Creator of Infinite Openings and Component Catalogues
Push the envelope & watch it bend
website: https://cadswift.com.au/
YouTube: https://www.youtube.com/user/CADSwift/playlists
Anonymous
Not applicable
Hi Kristian thanks for all the options still reading the variations you send. Thanks a lot, I will test and respond.
Anonymous
Not applicable
for I to N, forJ to FK we check Q.
If Q>0 then text2 0,-9-N-NS-I,I (I is a counter of columns in the array, J checks FK rows of the I column)
and after this ALSO text2 Q for H=1 to FK. If you notice it prints the Q values with horizontal step 3*H

If Q=0 then chek next column of Q array
Anonymous
Not applicable
could this be it?
FOR I=1 TO N
	  FOR J=1 TO FK
	            IF ABS(Q) >= EPS THEN
	                             TEXT2 0,-9-N-NS-I,I
	                  FOR H=1 TO FK
	                              TEXT2 3*H,-9-N-NS-I,Q							
	                  NEXT H  
                     ELSE
          NEXT J                                              
NEXT I