Click here to Skip to main content
15,844,607 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Feb 2011CPOL 6.4K   2   2
Alternate 8, why not just do this:bool bFailed = true;// In case of an exception.try{ // while bFailed while (bFailed) { // do something here. if (condition1) { // exit the loop (goto) bFalse = false; ...
Alternate 8, why not just do this:

C++
bool bFailed = true;
// In case of an exception.
try
{
    // while bFailed
    while (bFailed)
    {
        // do something here.
        if (condition1)
        {
            // exit the loop (goto)
            bFalse = false;
            break;
        }
        // do something here.
        // ....
        // In case you want to goto to the top instead of exit.
        if (condition2)
        {
            // jump to top of the loop. (goto)
            continue;
        }
        // Success.
        bFailed = false;

    }
}
catch (...)
{
    // this assumes you want to catch all.
}

if (bFailed)
{
    // do your error cleanup here.
}
// do your unconditional cleanup here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI was only looking at alternate 8 which uses a empty for loo... Pin
abridges7-Feb-11 4:57
abridges7-Feb-11 4:57 
I was only looking at alternate 8 which uses a empty for loop which I don't like, but the code will behave identical to Alternate 8 using a while loop.
GeneralI think that you have some problems with you code. First, i... Pin
sergiogarcianinja7-Feb-11 4:49
sergiogarcianinja7-Feb-11 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.