Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

The goto-less goto!

, 9 Feb 2011
Rate this:
Please Sign up or sign in to vote.
If you really have to put everything into a single function and want to keep the code analyzable and maybe want to be able put some common code at the end, you can always use a success variable. The code does not slow down, as the compiler optimizes the sequenced if-conditions away and produces...
If you really have to put everything into a single function and want to keep the code analyzable and maybe want to be able put some common code at the end, you can always use a success variable. The code does not slow down, as the compiler optimizes the sequenced if-conditions away and produces jumps to the end of the sequence (just checked on this in VS2008).
 
#include <iostream>
 
void Test( int aValue ) {
  bool  l_ok = true;
 
  std::cout << "starting Test " << aValue << std::endl;
  l_ok = aValue < 8;
 
  if (l_ok) {
    std::cout << "value < 8" << std::endl;
    l_ok = aValue > 5;
  }
 
  // add more cases here
  if (l_ok) {
    std::cout << "value > 5" << std::endl;
  }
  std::cout << "finishing Test success " << l_ok << std::endl;
}
 

int main( int argc, char * argv[] ) {
  int result = 0;
  int l_value;
 
  for (l_value = 0; l_value < 10; ++l_value) Test( l_value );
  return result;
}
</iostream>

License

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

Share

About the Author

Wolfgang_Baron
Technical Lead private interest
Germany Germany
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 9 Feb 2011
Article Copyright 2011 by Wolfgang_Baron
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid