Click here to Skip to main content
11,413,961 members (70,799 online)
Click here to Skip to main content

Tagged as

The goto-less goto!

, 24 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Ajay Vijayvargiya
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
Manikandan10, 6-Jun-14 21:52
professionalManikandan106-Jun-14 21:52 
GeneralMy vote of 1 Pin
Krzysztof Kalinowski, 12-Mar-13 22:25
memberKrzysztof Kalinowski12-Mar-13 22:25 
GeneralMy vote of 1 Pin
i00, 21-Oct-12 17:01
memberi0021-Oct-12 17:01 
GeneralReason for my vote of 2 No reason at all to use loop stateme... Pin
Albert Holguin, 14-Oct-11 6:01
memberAlbert Holguin14-Oct-11 6:01 
GeneralReason for my vote of 5 This is a very good alternate to usi... Pin
EFEaglehouse, 5-Apr-11 5:06
memberEFEaglehouse5-Apr-11 5:06 
GeneralReason for my vote of 1 Misused gotos has been replaced by m... Pin
Mass Nerder, 8-Feb-11 4:18
memberMass Nerder8-Feb-11 4:18 
GeneralReason for my vote of 5 I've looked at the many alternates, ... Pin
Carroll Arbogast, 7-Feb-11 14:01
memberCarroll Arbogast7-Feb-11 14:01 
GeneralReason for my vote of 3 Not very efficient, too many breaks. Pin
CIDev, 2-Feb-11 9:18
memberCIDev2-Feb-11 9:18 
GeneralReason for my vote of 1 IF you find yourself in this positio... Pin
Kirk Wood, 31-Jan-11 16:10
memberKirk Wood31-Jan-11 16:10 
GeneralRe: Now what Polymorphism has to do with this? Pin
Ajay Vijayvargiya, 2-Feb-11 16:57
memberAjay Vijayvargiya2-Feb-11 16:57 
GeneralGoto are not always bad... There are very specific cases whe... Pin
valery possoz, 31-Jan-11 13:46
membervalery possoz31-Jan-11 13:46 
GeneralRe: You are absolutely correct. An inspection of Dijkstra's pap... Pin
Ravi Bhavnani, 6-Apr-11 9:25
memberRavi Bhavnani6-Apr-11 9:25 
GeneralI don't use gotos either. My point was "how to avoid goto, i... Pin
Ajay Vijayvargiya, 24-Jan-11 17:15
memberAjay Vijayvargiya24-Jan-11 17:15 
GeneralRe: If you feel you need to use goto, I'd say you need to re-eva... Pin
Marcus Kramer, 26-Jan-11 15:54
memberMarcus Kramer26-Jan-11 15:54 
GeneralWhy not use if - else if - else if ...? You won't need to br... Pin
Indivara, 24-Jan-11 14:24
subeditorIndivara24-Jan-11 14:24 
GeneralGoto / multiple returns rules and alternatives Pin
Philippe Mori, 7-Apr-11 3:53
memberPhilippe Mori7-Apr-11 3:53 
General[My vote of 1] Pointless Pin
balab, 31-Jan-11 9:58
memberbalab31-Jan-11 9:58 
GeneralRe: [My vote of 1] Pointless Pin
wim ton, 31-Jan-11 22:30
memberwim ton31-Jan-11 22:30 
GeneralRe: [My vote of 1] Pointless Pin
mier, 7-Mar-11 23:43
membermier7-Mar-11 23:43 
GeneralMy vote of 1 Pin
SledgeHammer01, 24-Apr-10 9:23
memberSledgeHammer0124-Apr-10 9:23 
GeneralRe: My vote of 1 Pin
Ajay Vijayvargiya, 25-Apr-10 10:37
memberAjay Vijayvargiya25-Apr-10 10:37 
GeneralRe: My vote of 1 [modified] Pin
SledgeHammer01, 25-Apr-10 11:13
memberSledgeHammer0125-Apr-10 11:13 
GeneralRe: My vote of 1 Pin
supercat9, 26-Apr-10 7:31
membersupercat926-Apr-10 7:31 
GeneralRe: My vote of 1 Pin
Ajay Vijayvargiya, 26-Apr-10 19:37
memberAjay Vijayvargiya26-Apr-10 19:37 
GeneralFine but..... Pin
Abhinav S, 23-Apr-10 23:27
memberAbhinav S23-Apr-10 23:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.2 | Last Updated 24 Jan 2011
Article Copyright 2010 by Ajay Vijayvargiya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid