PDA

View Full Version : Goto nested if's



Sephir
07-25-2007, 11:17 PM
Hello. Another problem arises! :banghead: I want to make sub with some nested if's and for's and I want to return backwards if something happens (not correct var) and do again this procedure and go back if not right var, until correct variable is reached, and then continue...

Here is the code (just planning):

if a =1 then
for b = 1 to 100
do something...
Ret: if c =1 then
if d = 1 then
for e = 1 to 100
if x = 1 then
goto Ret
else
goto Cont
end if
Cont: if ....
next e
end if
end if
next b
end if


is this code OK? thank you

Please be patient, I am just newbie...

Bob Phillips
07-26-2007, 12:39 AM
I would strongly recommend that you DON'T implement code like, it is a debug nightmare. I cannot see how you will track down any problems that you encounter.

You should look at using Do While ... Loop or Do ... Until Loop as alternative approaches, or calling other procedures that do some of the work.

Sephir
07-26-2007, 04:13 AM
I would strongly recommend that you DON'T implement code like, it is a debug nightmare. I cannot see how you will track down any problems that you encounter.

You should look at using Do While ... Loop or Do ... Until Loop as alternative approaches, or calling other procedures that do some of the work. what do you mean with "calling other procedures that do some of the work"? Separate each if-then or for-next into own subs, functions?

Bob Phillips
07-26-2007, 04:18 AM
I can't really be specific as I have no idea what you are trying to achieve (quite honestly, the code in unreadable).

You would need to give me a specifice example.

Sephir
07-26-2007, 04:20 AM
I can't really be specific as I have no idea what you are trying to achieve (quite honestly, the code in unreadable).

You would need to give me a specifice example.

i don't have specific example. i am trying to figure out what is the best practicle to work with several nested if's, for's and how to jump back and forward in code using goto...

rory
07-26-2007, 04:23 AM
You should, as xld said, never jump back and forth in code like that, other than in an On Error Goto statement. If you need to exit a loop, use Exit Do, or Exit For.
Regards,
Rory

Sephir
07-26-2007, 04:30 AM
You should, as xld said, never jump back and forth in code like that, other than in an On Error Goto statement. If you need to exit a loop, use Exit Do, or Exit For.
Regards,
Rory

ok, but how to return in code? i mean this:

Ret: if c =1 then
if d = 1 then
for e = 1 to 100
if x = 1 then
goto Ret

rory
07-26-2007, 04:41 AM
You would use Exit For to get out of the For e = 1 to 100 loop. You would probably put the whole thing into a Do ... Loop or move the If d = 1... to If x = 1 bit into a separate procedure (or both) but unless you can explain what the code is supposed to do, we can't tell you how it should be written.