By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 429,044 Members | 1,287 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

# If else problem

 100+ P: 553 Basic if statement question: If ( .... ) { If (....) // If 1 *** If (....) // If 2 *** If (....) // If 3 *** } else if ( .... ) { if (something = something) { if (....) // *** This and the next 2 If statements are identical to those if (....) // in code above... how would you avoid duplicating the same if (....) // if statements.. here are only 3 but what if there are 10s? } } One solution to avoid duplicating the the same if statements is to use goto statement, but is there any othe rbetter and preferred way of doing it? Thanks Aug 4 '07 #1
Share this Question
10 Replies

 P: 41 Basic if statement question: If ( .... ) { If (....) // If 1 *** If (....) // If 2 *** If (....) // If 3 *** } else if ( .... ) { if (something = something) { if (....) // *** This and the next 2 If statements are identical to those if (....) // in code above... how would you avoid duplicating the same if (....) // if statements.. here are only 3 but what if there are 10s? } } One solution to avoid duplicating the the same if statements is to use goto statement, but is there any othe rbetter and preferred way of doing it? Thanks you could use a function: Expand|Select|Wrap|Line Numbers bool ifFunction(bool state) {    if(...)       if(...)          if(...)             return true;      return false; }   and then use it like this Expand|Select|Wrap|Line Numbers If  ( .... )  {    if(ifFunction(...)) } else     if ( .... )    {       if (something = something)       {          if(ifFunction(...))       }     }   Aug 4 '07 #2

 Expert Mod 5K+ P: 9,197 And you could make that function inline to avoid the overhead of a call. Aug 4 '07 #3

 Expert 10K+ P: 11,448 you could use a function: Expand|Select|Wrap|Line Numbers bool ifFunction(bool state) {    if(...)       if(...)          if(...)             return true;      return false; }   and then use it like this Expand|Select|Wrap|Line Numbers If  ( .... )  {    if(ifFunction(...)) } else     if ( .... )    {       if (something = something)       {          if(ifFunction(...))       }     }   How about a little Boolean algebra? T => (A , B , C) !T => (D , B , C) rewriting the terms yields T => A !T => D B C this reduces easily to code like this: Expand|Select|Wrap|Line Numbers if (T) A; else D; B; C;   kind regards, Jos Aug 4 '07 #4

 Expert Mod 5K+ P: 9,197 How about a little Boolean algebra? Ah yes. DeMorgan's Theorem and Karnaugh Maps. Check out Introduction to Boolean Algebra and Logic Design by Gerhard Hoernes (McGraw-Hill 1964). Aug 4 '07 #5

 Expert 10K+ P: 11,448 Ah yes. DeMorgan's Theorem and Karnaugh Maps. Check out Introduction to Boolean Algebra and Logic Design by Gerhard Hoernes (McGraw-Hill 1964). Yes, it's fun isn't it? Have a look at the Quine McCluskey (sp?) algorithm for a better, less human oriented, less visual approach at boolean logic formula minimization; those Karnaugh maps are a disaster in that respect. kind regards, Jos Aug 4 '07 #6

 Expert Mod 5K+ P: 9,197 less visual approach at boolean logic formula minimization; those Karnaugh maps are a disaster in that respect. Not if you use Gray Code. Aug 4 '07 #7

 Expert 10K+ P: 11,448 Not if you use Gray Code. Care to elaborate? because I don't understand how, e.g. a 3 variables Karaugh map (8 cells) would be organized such that a Gray code can come in handy. It might be just me because I've been lazy all day in my back garden ;-) kind regards, Jos (<-- no noticable brain activity to be found) Aug 4 '07 #8

 Expert Mod 5K+ P: 9,197 In a Gray code only one bit can change state to advance to the next code in the sequence. There are various forms but here is one: 000 001 011 010 110 111 101 100 By using a Gray code on the toroidal Karanugh map and plotting your true states of your Boolean equation, should any two true states be adjacant then one of the variables is irrelevant. That's because it changed state and did not affect the outcome. Four adjacent true points mean two irrelevant variables. And, of course, the open sequares are the inverse. It's much easier for reduction than using a Vietsch diagram and measuring Aug 4 '07 #9

 Expert 10K+ P: 11,448 In a Gray code only one bit can change state to advance to the next code in the sequence. There are various forms but here is one: 000 001 011 010 110 111 101 100 By using a Gray code on the toroidal Karanugh map and plotting your true states of your Boolean equation, should any two true states be adjacant then one of the variables is irrelevant. That's because it changed state and did not affect the outcome. Four adjacent true points mean two irrelevant variables. And, of course, the open sequares are the inverse. It's much easier for reduction than using a Vietsch diagram and measuring Yes I know what Gray codes are; but still, finding 'adjacent' areas is still a very visual process; the Q-McK algorithm simply produces tables of the terrms of disjunctive normal forms and produces a reduced table (if possible) etc. etc. until no further reduction is possible (it tries to find 'resolutions' just like those Gray code numbers). The Karnaugh maps make me dizzy above three (four?) variables; the Q-McK algorithm doesn't care about many variables are used. Doing that by hand is a boring and error prone process. I agree Karnaugh maps are more fun to work (play?) with for humans using just a few variables. kind regards, Jos Aug 4 '07 #10

 P: 8 Getting back to the question You can use compound expression in if Expand|Select|Wrap|Line Numbers Ex..   if (age >= 0)     if (age < 120)       print age is valid   to   if (age >= 0) && (age < 120)   print age is valid =========================   if (age < 0) || (age >= 120)   print age is valid   to   if (age < 0)    print age is valid else if (age >= 120)    peint age is valid Aug 4 '07 #11

### Post your reply

Sign in to post your reply or Sign up for a free account. 