Jump to content

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


Recommended Posts

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...

 

Link to comment
Share on other sites

I see no harm to exit a function like this.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.

Link to comment
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. :)

 

 

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...