Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does EXIT work with DO CASE?

Status
Not open for further replies.

dsandlin

Programmer
Jul 11, 2003
56
CR
I'm getting a Nesting Error message in a situation where I'm trying to use EXIT to escape from a CASE statement. The EXIT command is shown at the bottom of the help page for DO CASE in VFP8, but the description of the EXIT command only talks about DO WHILE, FOR, and SCAN loops.

My code looks something like this:

DO CASE
CASE Blah_Blah
lnX = 50
IF lnX > 10
EXIT
ENDIF
lnY = 30
CASE Blah_Blah_Blah
....
ENDCASE

I was expecting it to jump past the ENDCASE statement when it hit the EXIT command, but I'm getting a Nesting Error instead.

I'm trying to avoid pages and pages of 6-level indents that are tricky to keep track of and still have some semblance of structure.

Any observations? -- Dick [machinegun]
 
Why not just put the if statement at the bottom of the case rather than executing code after it? For example:

DO CASE
CASE Blah_Blah
lnX = 50
IF NOT lnX > 10 && Use keyword Not
lnY = 30
ENDIF

CASE Blah_Blah_Blah
....
ENDCASE


Also, if you are assigning the value of 50 to lnX then why wouldn't lnX be greater than 50?


-Kevin
 
Dick,

No, EXIT does not work with DO CASE. EXIT only works with the looping constructs, like DO WHILE and SCAN. You'll have to find an alternative way of achieving your goal, perhaps along the lines that Kevin suggested.

Brian's suggestion of using RETURN instead of EXIT will work if there is nothing in the routine after the ENDCASE. RETURN takes you right out of the routine, whereas EXIT only takes you out of the current loop.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Hi Dick,

I agree with Kevin. Once the code in CASE Blah_Blah is executed you'll exit the DO CASE statement anyway. Just order the code within each CASE appropriately.

Jim
 
Thanks to Kevin, Brian, Mike, and Jim for your posts.

Mike you confirm what I already feared to be true. I wonder why the help file lists EXIT at the bottom of the DO CASE page if it is not relevent?

My code snippet was just an example, and a poor one it seems. The actual code has many pages of indented statements and it is difficult to follow without drawing pencil lines to connect the loops. The EXIT would make it much easier to follow, if it worked.

RETURN can't be used here because there is code after the DO CASE that needs to run.


-- Dick
 
Dick,

I wonder why the help file lists EXIT at the bottom of the DO CASE page if it is not relevent

I think it's just a cross-reference to a related article (not that well related in this case).

The EXIT would make it much easier to follow, if it worked.

Don't forget that at most only one CASE is ever executed in a DO CASE. Once you have finished a given CASE branch, control passes to the line after the ENDCASE.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Hi Dick,

If you feel you really need the functionality of the EXIT statement you could wrap the entire DO CASE/ENDCASE in a FOR/ENDFOR. This would make the EXIT valid.

FOR i = 1 to 1
DO CASE
CASE blah
.
.
EXIT
.
CASE blahblah
.
ENDCASE
ENDFOR

Jim
 
Thanks, Mike and Jim, for the additional posts,

Jim, that is a clever cludge, but I'm afraid that it is a little too unorthodox for my coworkers to swallow.

I suppose I will live with the indented levels.


Thanks for your help! Dick [hairpull]
 
Another way is to have your case statements test for more specific matches, thereby reducing the number of nested IF statements. When using that logic it is important to note that the order of the tests may affect results. Usually place the most restrictive tests first.

There are 4 possible combinations when testing 2 variables, 9 combinations on 3 variables, etc.

Code:
DO CASE
    CASE Blah_1 AND Blah_2
        ....
    CASE NOT Blah_1 AND NOT Blah_2
        ....
    CASE Blah_1  && AND NOT Blah_2
        ....
    CASE Blah_2  && AND NOT Blah_1
        ....
ENDCASE
dbMark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top