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;
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
add a comment |
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
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 asyearsTotal *= 0.99
.
– Sulthan
Mar 22 at 10:54
add a comment |
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
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
swift if-statement
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 asyearsTotal *= 0.99
.
– Sulthan
Mar 22 at 10:54
add a comment |
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 asyearsTotal *= 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
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
You can use ternary operator to simplify this.
yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal
add a comment |
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
.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Mar 21 at 22:16
MochaMocha
7823 silver badges15 bronze badges
7823 silver badges15 bronze badges
add a comment |
add a comment |
You can use ternary operator to simplify this.
yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal
add a comment |
You can use ternary operator to simplify this.
yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal
add a comment |
You can use ternary operator to simplify this.
yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal
You can use ternary operator to simplify this.
yearsTotal = yearsTotal - (doesSmoke ? 7.5 : 0)
yearsTotal = (hoursSleepAvg < 7 || hoursSleepAvg > 8) ? (yearsTotal - yearsTotal / 100.0) : yearsTotal
answered Mar 22 at 10:08
AksAks
1,19511 silver badges22 bronze badges
1,19511 silver badges22 bronze badges
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Mar 22 at 10:56
SulthanSulthan
102k17 gold badges168 silver badges209 bronze badges
102k17 gold badges168 silver badges209 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 asyearsTotal *= 0.99
.– Sulthan
Mar 22 at 10:54