More efficient way of using lots of if else statementsPutting a simple if-then-else statement on one lineWhy does python use 'else' after for and while loops?More efficient way than using lots of else if statementsHow to implement if-else statement in XSLT?if else statement in AngularJS templatesMost efficient way of making an if-elif-elif-else statement when the else is done the most?What does an exclamation mark mean in the Swift language?Using isKindOfClass with SwiftUsing a dispatch_once singleton model in SwiftSwift Beta performance: sorting arrays

Averting Real Women Don’t Wear Dresses

Short story with brother-sister conjoined twins as protagonists?

Ending: accusative or not?

How come I was asked by a CBP officer why I was in the US?

Does squid ink pasta bleed?

Why is Madam Hooch not a professor?

Does the Paladin's Aura of Protection affect only either her or ONE ally in range?

What are the penalties for overstaying in USA?

Does Marvel have an equivalent of the Green Lantern?

What happens when your group is victim of a surprise attack but you can't be surprised?

Singing along to guitar chords (harmony)

Why does adding parentheses prevent an error?

A player is constantly pestering me about rules, what do I do as a DM?

What is the line crossing the Pacific Ocean that is shown on maps?

Why do some games show lights shine through walls?

Is there any set of 2-6 notes that doesn't have a chord name?

Are neural networks the wrong tool to solve this 2D platformer/shooter game? Is there a proven way to frame this problem to a neural network?

How can I repair scratches on a painted French door?

What can I do to find new work while my workplace is closed due to an accidental death?

Does anycast addressing add additional latency in any way?

Should I tell my insurance company I'm making payments on my new car?

Is adding a new player (or players) a DM decision, or a group decision?

Is it OK to bottle condition using previously contaminated bottles?

Why does the A-4 Skyhawk sit nose-up when on ground?



More efficient way of using lots of if else statements


Putting a simple if-then-else statement on one lineWhy does python use 'else' after for and while loops?More efficient way than using lots of else if statementsHow to implement if-else statement in XSLT?if else statement in AngularJS templatesMost efficient way of making an if-elif-elif-else statement when the else is done the most?What does an exclamation mark mean in the Swift language?Using isKindOfClass with SwiftUsing a dispatch_once singleton model in SwiftSwift Beta performance: sorting arrays






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I'm trying to do this in swift and I was wondering if there was a more efficient way:



if(doesSmoke)
yearsTotal = yearsTotal - 7.5

if hoursSleepAvg < 7
yearsTotal = yearsTotal - yearsTotal / 100.0

if hoursSleepAvg > 8 {
yearsTotal = yearsTotal - yearsTotal / 100.0


There are a lot more if statements. Does anyone have a solution?










share|improve this question
























  • Your last two checks seems to result in the same thing and are exclusive if each other, so you could just get rid of those checks.

    – Carcigenicate
    Mar 21 at 19:36












  • switch statement? How many other inputs do you have, besides doesSmoke & hoursSleepAvg?

    – William GP
    Mar 21 at 19:42






  • 2





    Sleeping 7.5 hours per night seems to be best.

    – Martin R
    Mar 21 at 19:57











  • yearsTotal = yearsTotal - yearsTotal / 100.0 can be also written as yearsTotal *= 0.99.

    – Sulthan
    Mar 22 at 10:54

















-1















I'm trying to do this in swift and I was wondering if there was a more efficient way:



if(doesSmoke)
yearsTotal = yearsTotal - 7.5

if hoursSleepAvg < 7
yearsTotal = yearsTotal - yearsTotal / 100.0

if hoursSleepAvg > 8 {
yearsTotal = yearsTotal - yearsTotal / 100.0


There are a lot more if statements. Does anyone have a solution?










share|improve this question
























  • Your last two checks seems to result in the same thing and are exclusive if each other, so you could just get rid of those checks.

    – Carcigenicate
    Mar 21 at 19:36












  • switch statement? How many other inputs do you have, besides doesSmoke & hoursSleepAvg?

    – William GP
    Mar 21 at 19:42






  • 2





    Sleeping 7.5 hours per night seems to be best.

    – Martin R
    Mar 21 at 19:57











  • yearsTotal = yearsTotal - yearsTotal / 100.0 can be also written as yearsTotal *= 0.99.

    – Sulthan
    Mar 22 at 10:54













-1












-1








-1








I'm trying to do this in swift and I was wondering if there was a more efficient way:



if(doesSmoke)
yearsTotal = yearsTotal - 7.5

if hoursSleepAvg < 7
yearsTotal = yearsTotal - yearsTotal / 100.0

if hoursSleepAvg > 8 {
yearsTotal = yearsTotal - yearsTotal / 100.0


There are a lot more if statements. Does anyone have a solution?










share|improve this question
















I'm trying to do this in swift and I was wondering if there was a more efficient way:



if(doesSmoke)
yearsTotal = yearsTotal - 7.5

if hoursSleepAvg < 7
yearsTotal = yearsTotal - yearsTotal / 100.0

if hoursSleepAvg > 8 {
yearsTotal = yearsTotal - yearsTotal / 100.0


There are a lot more if statements. Does anyone have a solution?







swift if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 10:59









JoRa

7156 silver badges21 bronze badges




7156 silver badges21 bronze badges










asked Mar 21 at 19:34









avidcellocoderavidcellocoder

1




1












  • Your last two checks seems to result in the same thing and are exclusive if each other, so you could just get rid of those checks.

    – Carcigenicate
    Mar 21 at 19:36












  • switch statement? How many other inputs do you have, besides doesSmoke & hoursSleepAvg?

    – William GP
    Mar 21 at 19:42






  • 2





    Sleeping 7.5 hours per night seems to be best.

    – Martin R
    Mar 21 at 19:57











  • yearsTotal = yearsTotal - yearsTotal / 100.0 can be also written as yearsTotal *= 0.99.

    – Sulthan
    Mar 22 at 10:54

















  • Your last two checks seems to result in the same thing and are exclusive if each other, so you could just get rid of those checks.

    – Carcigenicate
    Mar 21 at 19:36












  • switch statement? How many other inputs do you have, besides doesSmoke & hoursSleepAvg?

    – William GP
    Mar 21 at 19:42






  • 2





    Sleeping 7.5 hours per night seems to be best.

    – Martin R
    Mar 21 at 19:57











  • yearsTotal = yearsTotal - yearsTotal / 100.0 can be also written as yearsTotal *= 0.99.

    – Sulthan
    Mar 22 at 10:54
















Your last two checks seems to result in the same thing and are exclusive if each other, so you could just get rid of those checks.

– Carcigenicate
Mar 21 at 19:36






Your last two checks seems to result in the same thing and are exclusive if each other, so you could just get rid of those checks.

– Carcigenicate
Mar 21 at 19:36














switch statement? How many other inputs do you have, besides doesSmoke & hoursSleepAvg?

– William GP
Mar 21 at 19:42





switch statement? How many other inputs do you have, besides doesSmoke & hoursSleepAvg?

– William GP
Mar 21 at 19:42




2




2





Sleeping 7.5 hours per night seems to be best.

– Martin R
Mar 21 at 19:57





Sleeping 7.5 hours per night seems to be best.

– Martin R
Mar 21 at 19:57













yearsTotal = yearsTotal - yearsTotal / 100.0 can be also written as yearsTotal *= 0.99.

– Sulthan
Mar 22 at 10:54





yearsTotal = yearsTotal - yearsTotal / 100.0 can be also written as yearsTotal *= 0.99.

– Sulthan
Mar 22 at 10:54












3 Answers
3






active

oldest

votes


















0














You can consolidate different types of if statements into methods.



For example



main() 
yearsTotal += accountForSmoke()
yearsTotal += accountForSleep(yearsTotal)


func accountForSmoke() -> Double
return doesSmoke ? -7.5 : 0


func accountForSleep(_ yearsTotal: Double ) -> Double
if hoursSleepAvg < 7
return -yearsTotal / 100.0

if hoursSleepAvg > 8
return -yearsTotal / 100.0







share|improve this answer






























    0














    You can use ternary operator to simplify this.



    yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
    yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal





    share|improve this answer






























      0














      To simplify the logic as much as possible:



      if (doesSmoke)
      yearsTotal -= 7.5


      if hoursSleepAvg < 7 || hoursSleepAvg > 8
      yearsTotal *= 0.99



      However, the logic is very questionable. It seems obvious that hoursSleepAvg won't have the same effect when it's equal to 7.00 and when it's equal to 7.99.






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288075%2fmore-efficient-way-of-using-lots-of-if-else-statements%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        You can consolidate different types of if statements into methods.



        For example



        main() 
        yearsTotal += accountForSmoke()
        yearsTotal += accountForSleep(yearsTotal)


        func accountForSmoke() -> Double
        return doesSmoke ? -7.5 : 0


        func accountForSleep(_ yearsTotal: Double ) -> Double
        if hoursSleepAvg < 7
        return -yearsTotal / 100.0

        if hoursSleepAvg > 8
        return -yearsTotal / 100.0







        share|improve this answer



























          0














          You can consolidate different types of if statements into methods.



          For example



          main() 
          yearsTotal += accountForSmoke()
          yearsTotal += accountForSleep(yearsTotal)


          func accountForSmoke() -> Double
          return doesSmoke ? -7.5 : 0


          func accountForSleep(_ yearsTotal: Double ) -> Double
          if hoursSleepAvg < 7
          return -yearsTotal / 100.0

          if hoursSleepAvg > 8
          return -yearsTotal / 100.0







          share|improve this answer

























            0












            0








            0







            You can consolidate different types of if statements into methods.



            For example



            main() 
            yearsTotal += accountForSmoke()
            yearsTotal += accountForSleep(yearsTotal)


            func accountForSmoke() -> Double
            return doesSmoke ? -7.5 : 0


            func accountForSleep(_ yearsTotal: Double ) -> Double
            if hoursSleepAvg < 7
            return -yearsTotal / 100.0

            if hoursSleepAvg > 8
            return -yearsTotal / 100.0







            share|improve this answer













            You can consolidate different types of if statements into methods.



            For example



            main() 
            yearsTotal += accountForSmoke()
            yearsTotal += accountForSleep(yearsTotal)


            func accountForSmoke() -> Double
            return doesSmoke ? -7.5 : 0


            func accountForSleep(_ yearsTotal: Double ) -> Double
            if hoursSleepAvg < 7
            return -yearsTotal / 100.0

            if hoursSleepAvg > 8
            return -yearsTotal / 100.0








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 21 at 22:16









            MochaMocha

            7823 silver badges15 bronze badges




            7823 silver badges15 bronze badges























                0














                You can use ternary operator to simplify this.



                yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
                yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal





                share|improve this answer



























                  0














                  You can use ternary operator to simplify this.



                  yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
                  yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal





                  share|improve this answer

























                    0












                    0








                    0







                    You can use ternary operator to simplify this.



                    yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
                    yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal





                    share|improve this answer













                    You can use ternary operator to simplify this.



                    yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
                    yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 22 at 10:08









                    AksAks

                    1,19511 silver badges22 bronze badges




                    1,19511 silver badges22 bronze badges





















                        0














                        To simplify the logic as much as possible:



                        if (doesSmoke)
                        yearsTotal -= 7.5


                        if hoursSleepAvg < 7 || hoursSleepAvg > 8
                        yearsTotal *= 0.99



                        However, the logic is very questionable. It seems obvious that hoursSleepAvg won't have the same effect when it's equal to 7.00 and when it's equal to 7.99.






                        share|improve this answer



























                          0














                          To simplify the logic as much as possible:



                          if (doesSmoke)
                          yearsTotal -= 7.5


                          if hoursSleepAvg < 7 || hoursSleepAvg > 8
                          yearsTotal *= 0.99



                          However, the logic is very questionable. It seems obvious that hoursSleepAvg won't have the same effect when it's equal to 7.00 and when it's equal to 7.99.






                          share|improve this answer

























                            0












                            0








                            0







                            To simplify the logic as much as possible:



                            if (doesSmoke)
                            yearsTotal -= 7.5


                            if hoursSleepAvg < 7 || hoursSleepAvg > 8
                            yearsTotal *= 0.99



                            However, the logic is very questionable. It seems obvious that hoursSleepAvg won't have the same effect when it's equal to 7.00 and when it's equal to 7.99.






                            share|improve this answer













                            To simplify the logic as much as possible:



                            if (doesSmoke)
                            yearsTotal -= 7.5


                            if hoursSleepAvg < 7 || hoursSleepAvg > 8
                            yearsTotal *= 0.99



                            However, the logic is very questionable. It seems obvious that hoursSleepAvg won't have the same effect when it's equal to 7.00 and when it's equal to 7.99.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 22 at 10:56









                            SulthanSulthan

                            102k17 gold badges168 silver badges209 bronze badges




                            102k17 gold badges168 silver badges209 bronze badges



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288075%2fmore-efficient-way-of-using-lots-of-if-else-statements%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript