Click here to Skip to main content
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 PinprofessionalManikandan106-Jun-14 21:52 
GeneralMy vote of 1 PinmemberKrzysztof Kalinowski12-Mar-13 22:25 
GeneralMy vote of 1 Pinmemberi0021-Oct-12 17:01 
GeneralReason for my vote of 2 No reason at all to use loop stateme... PinmemberAlbert Holguin14-Oct-11 6:01 
GeneralReason for my vote of 5 This is a very good alternate to usi... PinmemberEFEaglehouse5-Apr-11 5:06 
GeneralReason for my vote of 1 Misused gotos has been replaced by m... PinmemberMass Nerder8-Feb-11 4:18 
GeneralReason for my vote of 5 I've looked at the many alternates, ... PinmemberCarroll Arbogast7-Feb-11 14:01 
GeneralReason for my vote of 3 Not very efficient, too many breaks. PinmemberCIDev2-Feb-11 9:18 
GeneralReason for my vote of 1 IF you find yourself in this positio... PinmemberKirk Wood31-Jan-11 16:10 
GeneralRe: Now what Polymorphism has to do with this? PinmemberAjay Vijayvargiya2-Feb-11 16:57 
GeneralGoto are not always bad... There are very specific cases whe... Pinmembervalery possoz31-Jan-11 13:46 
GeneralRe: You are absolutely correct. An inspection of Dijkstra's pap... PinmemberRavi Bhavnani6-Apr-11 9:25 
GeneralI don't use gotos either. My point was "how to avoid goto, i... PinmemberAjay Vijayvargiya24-Jan-11 17:15 
GeneralRe: If you feel you need to use goto, I'd say you need to re-eva... PinmemberMarcus Kramer26-Jan-11 15:54 
GeneralWhy not use if - else if - else if ...? You won't need to br... PinsubeditorIndivara24-Jan-11 14:24 
GeneralGoto / multiple returns rules and alternatives PinmemberPhilippe Mori7-Apr-11 3:53 
General[My vote of 1] Pointless Pinmemberbalab31-Jan-11 9:58 
GeneralRe: [My vote of 1] Pointless Pinmemberwim ton31-Jan-11 22:30 
GeneralRe: [My vote of 1] Pointless Pinmembermier7-Mar-11 23:43 
GeneralMy vote of 1 PinmemberSledgeHammer0124-Apr-10 9:23 
GeneralRe: My vote of 1 PinmemberAjay Vijayvargiya25-Apr-10 10:37 
GeneralRe: My vote of 1 [modified] PinmemberSledgeHammer0125-Apr-10 11:13 
GeneralRe: My vote of 1 Pinmembersupercat926-Apr-10 7:31 
GeneralRe: My vote of 1 PinmemberAjay Vijayvargiya26-Apr-10 19:37 
GeneralFine but..... PinmemberAbhinav S23-Apr-10 23:27 
GeneralRe: Fine but..... PinmemberAjay Vijayvargiya23-Apr-10 23:35 

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
Web04 | 2.8.141220.1 | Last Updated 24 Jan 2011
Article Copyright 2010 by Ajay Vijayvargiya
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid