Switch statement for greater-than/less-than
When I looked at the solutions in the other answers I saw some things that I know are bad for performance. I was going to put them in a comment but I thought it was better to benchmark it and share the results. You can test it yourself. Below are my results (ymmv) normalized after the fastest operation in each browser.
Here is the results from 2021-MAY-05
Test | Chrome | Firefox | Opera | Edge | Brave | Node |
---|---|---|---|---|---|---|
1.0 time | 15 ms | 14 ms | 17 ms | 17 ms | 16 ms | 14 ms |
if-immediate | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
if-indirect | 2.20 | 1.21 | 2.06 | 2.18 | 2.19 | 1.93 |
switch-immediate | 2.07 | 1.43 | 1.71 | 1.71 | 2.19 | 1.93 |
switch-range | 3.60 | 2.00 | 2.47 | 2.65 | 2.88 | 2.86 |
switch-range2 | 2.07 | 1.36 | 1.82 | 1.71 | 1.94 | 1.79 |
switch-indirect-array | 2.93 | 1.57 | 2.53 | 2.47 | 2.75 | 2.50 |
array-linear-switch | 2.73 | 3.29 | 2.12 | 2.12 | 2.38 | 2.50 |
array-binary-switch | 5.80 | 6.07 | 5.24 | 5.24 | 5.44 | 5.37 |
Larger than and less than in C switch statement
There is no clean way to solve this with switch, as cases need to be integral types. Have a look at if-else if-else.
switch case of greater than or less than variable
You can use switch (true)
:
switch (true) {
case sum === 0:
alert('success');
break;
case sum < 50:
alert('also success');
break;
case sum > 50:
alert('failed');
break;
default:
alert('there is an error.')
break;
}
Note that in your code, the first case is actually an assignment and modify sum
to set it to 0.
Switch Case Statement With Greater Than Operator?
/**
* bendingSetupHours
* @customfunction bendingSetupHours
* @param {number} numOfBends
* @param {number} bendLengthInches
* @returns {number} bendingSetupHours
*/
function bendingSetupHours(numOfBends, bendLengthInches) {
var machine;
var setupMins;
switch(bendLengthInches) {
case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16:
case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 27: case 29: case 30: case 31:
case 32: case 33: case 34: case 45: case 36:
machine = 1;
break;
case 37: case 38: case 39: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49: case 50: case 51:
case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: case 60: case 61: case 62: case 63: case 64: case 65: case 66:
case 67: case 68: case 69: case 70: case 71:
machine = 2;
break;
case "":
machine = 0;
break;
default:
machine = 3;
}
if (machine == 1 && numOfBends == 0 || numOfBends == 1 || numOfBends == 2 || numOfBends == 3) {
setupMins = 15;
} else if (machine == 1 && numOfBends == 5 || numOfBends == 6 || numOfBends == 7) {
setupMins = 30;
} else if (machine == 1 && numOfBends > 7) {
setupMins = 60;
} else if (machine == 2 && numOfBends == 0 || numOfBends == 1 || numOfBends == 2 || numOfBends == 3) {
setupMins = 15;
} else if (machine == 2 && numOfBends == 4 || numOfBends == 5 || numOfBends == 6 || numOfBends == 7) {
setupMins = 45;
} else if (machine == 2 && numOfBends > 7) {
setupMins = 90;
} else if (machine == 3 && numOfBends == 0 || numOfBends == 1 || numOfBends == 2 || numOfBends == 3) {
setupMins = 30;
} else if (machine == 3 && numOfBends == 4 || numOfBends == 5 || numOfBends == 6 || numOfBends == 7) {
setupMins = 60;
} else if (machine == 3 && numOfBends > 7) {
setupMins = 120;
} else {
setupMins = 0;
}
return (setupMins / 60);
}
How to use greater than or equal in a switch statement
Not sure if this is what you're asking, but you could do it this way:
int var1;
int var2;
int signum = Long.signum((long)var1 - var2);
switch(signum) {
case -1: break;
case 0: break;
case 1: break;
}
Lesser than or greater than in Swift switch statement
Here's one approach. Assuming someVar
is an Int
or other Comparable
, you can optionally assign the operand to a new variable. This lets you scope it however you want using the where
keyword:
var someVar = 3
switch someVar {
case let x where x < 0:
print("x is \(x)")
case let x where x == 0:
print("x is \(x)")
case let x where x > 0:
print("x is \(x)")
default:
print("this is impossible")
}
This can be simplified a bit:
switch someVar {
case _ where someVar < 0:
print("someVar is \(someVar)")
case 0:
print("someVar is 0")
case _ where someVar > 0:
print("someVar is \(someVar)")
default:
print("this is impossible")
}
You can also avoid the where
keyword entirely with range matching:
switch someVar {
case Int.min..<0:
print("someVar is \(someVar)")
case 0:
print("someVar is 0")
default:
print("someVar is \(someVar)")
}
Related Topics
How to Tell If Caps Lock Is on Using JavaScript
Finding Matches Between Multiple JavaScript Arrays
Getattribute() Versus Element Object Properties
Setting CSS Value Limits of the Window Scrolling Animation
Force Browser to Trigger Reflow While Changing CSS
Maximum Item Size in Indexeddb
Why Does Jquery Only Affect the First Div Element
Html5 Audio Tag on Safari Has a Delay
Multicolor Text Highlighting in a Textarea or Text Input
Uploading Multiple Files to Google Drive with Google App Script
Document.Getelementsbyclassname().Innerhtml Always Returns "Undefined"
How to Add Custom HTML Attributes in Jsx
Jquery Select Option Click Handler
How to Determine If a Checkbox Is Checked
Property 'Submit' of Object #<Htmlformelement> Is Not a Function