mbunds

Is it OK to exit a "switch" statement with "Return"?

4 posts in this topic

Back in my early days programming GWbasic, I caused stack-overflow errors by exiting for-next loops ungracefully with "goto" commands.

In those days, there was no "exitfor", no "Do-While/Until", no "Select-Case"; if I remember correctly, when we wanted to exit a loop prematurely, we had to "goto" a routine that would "pop" unused iterations off of the stack.

I have an example script that "returns" out of a case statement, and am wondering if this is acceptable form?

(Snippet from Henry Aymans "Phone Book Rev 02")

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Shtdown()
                Return
            Case $cmdNext
                cmdNext_Click()
            Case $cmdPrev...

 

Share this post


Link to post
Share on other sites



I see no harm to exit a function like this.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
8 minutes ago, water said:

I see no harm to exit a function like this.

Thank you kindly! 

I wanted to be sure because I didn't learn about my error in GWbasic until the program had been running for a considerable amount of time.

Share this post


Link to post
Share on other sites
18 hours ago, mbunds said:

Thank you kindly! 

I wanted to be sure because I didn't learn about my error in GWbasic until the program had been running for a considerable amount of time.

I didn't learn about my error programming using GWBasic for a long time!!  heh heh

Just kidding.  I also did QuickBasic 3.0 for a while.  Now if I try to use FreeBasic I struggle with the syntax.  It has so many compatibility modes that just about every function call syntax I try looks right but generates a syntax error. :)

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now