if...else...finally? question about style

This is a discussion on if...else...finally? question about style within the C Programming forums, part of the General Programming Boards category; Originally Posted by Nyda Code: /*bool a,b,c maybe passed in by parameters*/ bool entered= false; if (a) { ... code ...

  1. #16
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Nyda
    Code:
    /*bool a,b,c maybe passed in by parameters*/
    bool entered= false;
    if (a) {
      ... code for section a....
      entered= true;
    }else if (b) {
      ...code for section b....
      entered= true;
    }else if (c) {
      ...code for section c....
      entered= true;
    }
    
    if (entered) {
      ... common code for all sections ...
    }
    Maybe
    Code:
    /*bool a,b,c maybe passed in by parameters*/
    if (a) {
      ... code for section a....
    }else if (b) {
      ...code for section b....
    }else if (c) {
      ...code for section c....
    } else 
        return;//if you're not going to do anything if these tests fail...
    
      ... common code for all sections ...

  2. #17
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Imagine "Common code" is several lines of code which depends on too many variables from the current function to export it to its own function.
    Oh yeah almost forgot about this: structs are your friend.

  3. #18
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by xErath
    Maybe
    Code:
    /*bool a,b,c maybe passed in by parameters*/
    if (a) {
      ... code for section a....
    }else if (b) {
      ...code for section b....
    }else if (c) {
      ...code for section c....
    } else 
        return;//if you're not going to do anything if these tests fail...
    
      ... common code for all sections ...
    If common code was the last thing to execute that would be a neat solution, but it's certainly something to keep in mind, thanks!

    Quote Originally Posted by Thantos
    Oh yeah almost forgot about this: structs are your friend.
    Yep, I was just generalizing the problem. In any case, it's not like I got thousands of occurances of this situations. I just thought someone might know of a clever way to solve it. Thanks for everybody's suggestions!
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. automatic type conversation style question
    By sept in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 04:28 AM
  2. [MFC] Picture box question (CStatic with SS_BITMAP style)
    By Lionel in forum Windows Programming
    Replies: 2
    Last Post: 09-29-2007, 12:13 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 07:22 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21