Advertisement
Guest User

Is goto okay?

a guest
May 10th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. int useGoto()
  2. {
  3.     if(fun1())
  4.         goto errorCleanup;
  5.     doit();
  6.    
  7.     if(fun2())
  8.         goto errorCleanup;
  9.     doit();
  10.    
  11.     if(fun3())
  12.         goto errorCleanup;
  13.     doit();
  14.  
  15.     return 0;
  16.    
  17.     errorCleanup:
  18.     cleanup1();
  19.     cleanup2();
  20.     cleanup3();
  21.     return 1;
  22. }
  23.  
  24. int nestedIf()
  25. {
  26.     if(fun1() == 0)
  27.     {
  28.         doit();
  29.         if(fun2() == 0)
  30.         {
  31.             doit();
  32.             if(fun3() == 0)
  33.             {
  34.                 doit();
  35.             }
  36.             else
  37.             {
  38.                 cleanup1();
  39.                 cleanup2();
  40.                 cleanup3();
  41.                 return 1;
  42.             }
  43.         }
  44.         else
  45.         {
  46.             cleanup1();
  47.             cleanup2();
  48.             cleanup3();
  49.             return 1;
  50.         }
  51.     }
  52.     else
  53.     {
  54.         cleanup1();
  55.         cleanup2();
  56.         cleanup3();
  57.         return 1;
  58.     }
  59.     return 0;
  60. }
  61.  
  62. int singleIfBoolean()
  63. {
  64.     if(fun1() && (doit(), 1) && fun2() && (doit(), 1) && fun3() && (doit(), 1))
  65.     {
  66.         return 0;
  67.     }
  68.     else
  69.     {
  70.         cleanup1();
  71.         cleanup2();
  72.         cleanup3();
  73.         return 1;
  74.     }
  75. }
  76.  
  77. int nonNestedIfButRepeatedCode()
  78. {
  79.     if(fun1() != 0)
  80.     {
  81.         cleanup1();
  82.         cleanup2();
  83.         cleanup3();
  84.         return 1;
  85.     }
  86.    
  87.     doit();
  88.    
  89.     if(fun2() != 0)
  90.     {
  91.         cleanup1();
  92.         cleanup2();
  93.         cleanup3();
  94.         return 1;
  95.     }
  96.        
  97.     doit();
  98.    
  99.     if(fun3() != 0)
  100.     {
  101.         cleanup1();
  102.         cleanup2();
  103.         cleanup3();
  104.         return 1;
  105.     }
  106.    
  107.     doit();
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement