Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Tip/Trick

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
3.34/5 (23 votes)
24 Jan 2011CPOL 81.4K   9   26
The goto-less goto!
You have non-looped, sequenced code, like below:

  bool bFailed = true;
  if(condition1_fails)
     goto Exit;
  ...
  if(condition2_fails)
     goto Exit;
  ...
  ...
  if(conditionN_fails)
     goto Exit;

  bFailed=false;
  PerformActionOnAllSuccess();

Exit:
  if(bFailed)
     DoFailedCleanup();
  else
     DoNormalCleanup()

Here is the goto-less goto, that you can use:
bool bFailed=true;
do
{
  if(condition1_fails)
     break;
  ...
  if(condition2_fails)
     break;
  ...
  ...
  if(conditionN_fails)
     break;

   bFailed = false;
   PerformActionOnAllSuccess();

 } while(false); // Ignore or disable warning at function-level

 if(bFailed)
    DoFailedCleanup();
 else
    DoNormalCleanup();

It runs a do-while loop, which would run only once. As soon as some failed-condition encounters, it exits (break) from loop.

License

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


Written By
Software Developer (Senior)
India India
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

Comments and Discussions

 
GeneralMy vote of 3 Pin
Manikandan106-Jun-14 20:52
professionalManikandan106-Jun-14 20:52 
GeneralMy vote of 1 Pin
Krzysztof Kalinowski12-Mar-13 21:25
Krzysztof Kalinowski12-Mar-13 21:25 
GeneralMy vote of 1 Pin
i0021-Oct-12 16:01
i0021-Oct-12 16:01 
GeneralReason for my vote of 2 No reason at all to use loop stateme... Pin
Albert Holguin14-Oct-11 5:01
professionalAlbert Holguin14-Oct-11 5:01 
GeneralReason for my vote of 5 This is a very good alternate to usi... Pin
EFEaglehouse5-Apr-11 4:06
EFEaglehouse5-Apr-11 4:06 
GeneralReason for my vote of 1 Misused gotos has been replaced by m... Pin
Mass Nerder8-Feb-11 3:18
Mass Nerder8-Feb-11 3:18 
GeneralReason for my vote of 5 I've looked at the many alternates, ... Pin
Carroll Arbogast7-Feb-11 13:01
Carroll Arbogast7-Feb-11 13:01 
GeneralReason for my vote of 3 Not very efficient, too many breaks. Pin
BillW332-Feb-11 8:18
professionalBillW332-Feb-11 8:18 
GeneralReason for my vote of 1 IF you find yourself in this positio... Pin
Kirk Wood31-Jan-11 15:10
Kirk Wood31-Jan-11 15:10 
GeneralRe: Now what Polymorphism has to do with this? Pin
Ajay Vijayvargiya2-Feb-11 15:57
Ajay Vijayvargiya2-Feb-11 15:57 
GeneralGoto are not always bad... There are very specific cases whe... Pin
Valery Possoz31-Jan-11 12:46
professionalValery Possoz31-Jan-11 12:46 
GeneralRe: You are absolutely correct. An inspection of Dijkstra's pap... Pin
Ravi Bhavnani6-Apr-11 8:25
professionalRavi Bhavnani6-Apr-11 8:25 
GeneralI don't use gotos either. My point was "how to avoid goto, i... Pin
Ajay Vijayvargiya24-Jan-11 16:15
Ajay Vijayvargiya24-Jan-11 16:15 
GeneralRe: If you feel you need to use goto, I'd say you need to re-eva... Pin
fjdiewornncalwe26-Jan-11 14:54
professionalfjdiewornncalwe26-Jan-11 14:54 
GeneralWhy not use if - else if - else if ...? You won't need to br... Pin
Indivara24-Jan-11 13:24
professionalIndivara24-Jan-11 13:24 
GeneralGoto / multiple returns rules and alternatives Pin
Philippe Mori7-Apr-11 2:53
Philippe Mori7-Apr-11 2:53 
General[My vote of 1] Pointless Pin
balab31-Jan-11 8:58
balab31-Jan-11 8:58 
GeneralRe: [My vote of 1] Pointless Pin
wim ton31-Jan-11 21:30
wim ton31-Jan-11 21:30 
GeneralRe: [My vote of 1] Pointless Pin
mier7-Mar-11 22:43
mier7-Mar-11 22:43 
That's just ridiculous. I agree that using goto is generally not good practice, but it's far more preferable than using a do...while or for loop.
GeneralMy vote of 1 Pin
SledgeHammer0124-Apr-10 8:23
SledgeHammer0124-Apr-10 8:23 
GeneralRe: My vote of 1 Pin
Ajay Vijayvargiya25-Apr-10 9:37
Ajay Vijayvargiya25-Apr-10 9:37 
GeneralRe: My vote of 1 [modified] Pin
SledgeHammer0125-Apr-10 10:13
SledgeHammer0125-Apr-10 10:13 
GeneralRe: My vote of 1 Pin
supercat926-Apr-10 6:31
supercat926-Apr-10 6:31 
GeneralRe: My vote of 1 Pin
Ajay Vijayvargiya26-Apr-10 18:37
Ajay Vijayvargiya26-Apr-10 18:37 
GeneralFine but..... Pin
Abhinav S23-Apr-10 22:27
Abhinav S23-Apr-10 22:27 

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.