Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to avoid if else...

How to avoid if else...

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialcode-review
21 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V vikasvds

    Hello, previously i have posted in wrong forum. Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

    vicky

    M Offline
    M Offline
    molesworth
    wrote on last edited by
    #5

    You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement. There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

    There are three kinds of people in the world - those who can count and those who can't...

    L L 2 Replies Last reply
    0
    • M molesworth

      You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement. There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

      There are three kinds of people in the world - those who can count and those who can't...

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #6

      molesworth wrote:

      There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

      Here are some quotes from Kent Beck in his book Implementation Patterns[^]

      If/then and switch statements are the simplest form of instance –specific behavior…. The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated. These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.

      M C 2 Replies Last reply
      0
      • V vikasvds

        Hello, previously i have posted in wrong forum. Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

        vicky

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #7

        Have you something personal against if-else?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        V 1 Reply Last reply
        0
        • L led mike

          molesworth wrote:

          There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

          Here are some quotes from Kent Beck in his book Implementation Patterns[^]

          If/then and switch statements are the simplest form of instance –specific behavior…. The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated. These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.

          M Offline
          M Offline
          molesworth
          wrote on last edited by
          #8

          led mike wrote:

          The more paths through a program the less likely the program is to be correct

          Aye, that's very true, and anything that can reduce complexity will also reduce the likelihood of bugs, and improve readability and, more importantly, testability. However, sometimes there's no way to improve on an if/else construct, and when used sensibly they're the right tools for the job. When you get into a morass of nested if/elseif/elseif... blocks, then you certainly do introduce problems. Sheesh! Next thing you'll be telling me "goto" is a bad idea :)

          There are three kinds of people in the world - those who can count and those who can't...

          1 Reply Last reply
          0
          • L led mike

            molesworth wrote:

            There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

            Here are some quotes from Kent Beck in his book Implementation Patterns[^]

            If/then and switch statements are the simplest form of instance –specific behavior…. The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated. These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #9

            I wouldn't subclass a if. :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            L 1 Reply Last reply
            0
            • C CPallini

              I wouldn't subclass a if. :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #10

              Why not? It's simple class MyIf : public if { }; IT'S FRIDAY!!!!! :beer:

              C 1 Reply Last reply
              0
              • M molesworth

                You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement. There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

                There are three kinds of people in the world - those who can count and those who can't...

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #11

                molesworth wrote:

                There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...

                Adding elses to this code would dramatically change it's flow. Note that for n in a range (1,10) all callfuncs would be called.

                Greetings - Jacek Gajek

                1 Reply Last reply
                0
                • L led mike

                  Why not? It's simple class MyIf : public if { }; IT'S FRIDAY!!!!! :beer:

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #12

                  And now saturday here :beer: :beer: :pizza: :beer: :beer: :whisky:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  L 1 Reply Last reply
                  0
                  • V vikasvds

                    Hello, previously i have posted in wrong forum. Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

                    vicky

                    A Offline
                    A Offline
                    Arun Singh K
                    wrote on last edited by
                    #13

                    You can try something like this int func1() { return 0; } int func2() { return 0; } typedef int (*funcptr)(); struct condition { int low; int high; funcptr func; }; condition arr[] = { { 10, 20, func1 }, { 20, 30, func2 } // Can add more data here }; int n=17; for (int i=0;i<2;++i) { if (n > arr[i].low && n < arr[i].high) { arr[i].func(); // If you want you can break here } } -Arun

                    V 1 Reply Last reply
                    0
                    • A Arun Singh K

                      You can try something like this int func1() { return 0; } int func2() { return 0; } typedef int (*funcptr)(); struct condition { int low; int high; funcptr func; }; condition arr[] = { { 10, 20, func1 }, { 20, 30, func2 } // Can add more data here }; int n=17; for (int i=0;i<2;++i) { if (n > arr[i].low && n < arr[i].high) { arr[i].func(); // If you want you can break here } } -Arun

                      V Offline
                      V Offline
                      vikasvds
                      wrote on last edited by
                      #14

                      Thank you very much all of your response, The approach Arun has suggested is i think the way i was looking for as best and fasted approach. Thanks Arun to help me suggesting this approach, it's really nice and definitely fastest method uses full potential of C/C++ language. Thanks again. Vikas

                      vicky

                      V 1 Reply Last reply
                      0
                      • V vikasvds

                        Thank you very much all of your response, The approach Arun has suggested is i think the way i was looking for as best and fasted approach. Thanks Arun to help me suggesting this approach, it's really nice and definitely fastest method uses full potential of C/C++ language. Thanks again. Vikas

                        vicky

                        V Offline
                        V Offline
                        vikasvds
                        wrote on last edited by
                        #15

                        One more advantage of this method i can change limits any time at one location for any if condition. So to change the limits i need to see only at one place. Thanks Vikas

                        vicky

                        1 Reply Last reply
                        0
                        • L led mike

                          You ignored the responses you got on this question the first time. Reposting is rude. Perhaps you can explain why you ignored the previous responses and still reposted the same question.

                          V Offline
                          V Offline
                          vikasvds
                          wrote on last edited by
                          #16

                          I am very sorry for my re-post. I have re-posted my question because at other discussion thread i was getting responses in terms of C# and .Net, but i need response in terms of pure C. I have not done it intentionally, because of web page movement by mistake i've clicked at C# discussion link and posted the question instead of C/C++ category. Thank Vikas

                          vicky

                          L 1 Reply Last reply
                          0
                          • C CPallini

                            Have you something personal against if-else?

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            V Offline
                            V Offline
                            vikasvds
                            wrote on last edited by
                            #17

                            CPallini wrote:

                            Have you something personal against if-else?

                            Yes, i don't like if-else I hate them X|

                            vicky

                            C 1 Reply Last reply
                            0
                            • V vikasvds

                              CPallini wrote:

                              Have you something personal against if-else?

                              Yes, i don't like if-else I hate them X|

                              vicky

                              C Offline
                              C Offline
                              CPallini
                              wrote on last edited by
                              #18

                              Try

                              #define WHATEVER_YOU_LIKE_AS_IF if
                              #define WHATEVER_YOU_LIKE_AS_ELSE else

                              and then, for instance

                              WHATEVER_YOU_LIKE_AS_IF (i>0 && i<10)
                              //..
                              WHATEVER_YOU_LIKE_AS_ELSE WHATEVER_YOU_LIKE_AS_IF (i>11 && i<20)
                              //..
                              //...

                              ;P

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              1 Reply Last reply
                              0
                              • C CPallini

                                And now saturday here :beer: :beer: :pizza: :beer: :beer: :whisky:

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                L Offline
                                L Offline
                                led mike
                                wrote on last edited by
                                #19

                                Wow, why would someone vote this a '1'? :omg: :wtf:

                                C 1 Reply Last reply
                                0
                                • V vikasvds

                                  I am very sorry for my re-post. I have re-posted my question because at other discussion thread i was getting responses in terms of C# and .Net, but i need response in terms of pure C. I have not done it intentionally, because of web page movement by mistake i've clicked at C# discussion link and posted the question instead of C/C++ category. Thank Vikas

                                  vicky

                                  L Offline
                                  L Offline
                                  led mike
                                  wrote on last edited by
                                  #20

                                  vikasvds wrote:

                                  I am very sorry for my re-post.

                                  Ok, but have you begun to understand that OOD/OOP has solutions to writing conditional code?

                                  1 Reply Last reply
                                  0
                                  • L led mike

                                    Wow, why would someone vote this a '1'? :omg: :wtf:

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #21

                                    I suppose it is a personal attack against pizza :rolleyes:, so, basically, ignorance... :-D [added]

                                    led mike wrote:

                                    Wow, why would someone vote this a '1'?

                                    ...when is available a really idiotic post to target [^]? :laugh: [/added]

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups