✅ If else statement problem
Even or odd number
I got a problem to write an app to specify every digit from a number of 4 digits if its even or odd and I have no idea how to specify in code
27 Replies
Example : 1256
odd
even
odd
even
well there are 2 options in my mind
TheRanger#3357
REPL Result: Success
Console Output
Compile: 673.014ms | Execution: 45.053ms | React with ❌ to remove this embed.
ud have to convert 1256 to string first, then iterate through each character, then convert the char to int
But if I had to put this in a inputdata ?
take the string from the input data then
ok
thanks
But in case i have to transform all this in if else how I do it?
use if else instead of ? and :
its not hard
TheRanger#3357
REPL Result: Success
Console Output
Compile: 570.023ms | Execution: 85.246ms | React with ❌ to remove this embed.
this is better i think:
Dont
that's just ugly and bad practice
number % 2 == 0 is already a thing
can handle every number the computer can handle
ok, you are right, I should have used a switch. I will test performance test it against %. I think it could be faster. give me a minute
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I solved the problems
Thx guys
ok, i tested it with benchmark.net, this is the result: | Method | Input | Mean | Error | StdDev | Median |
|------- |------ |----------:|----------:|----------:|----------:|
| Switch | 0 | 1.7937 ns | 0.1157 ns | 0.2539 ns | 1.6716 ns |
| IfElse | 0 | 1.4684 ns | 0.0451 ns | 0.0377 ns | 1.4630 ns |
| Modulo | 0 | 1.1612 ns | 0.0829 ns | 0.1315 ns | 1.1269 ns |
looks like Modulo is faster
ehhhm both the switch and the ifelse approach should be using a modulo operator...
What you actually wanted to benchmark I guess is ifelse vs. switch vs. ternary operator
all these use modulo
Lol
This makes me think of that IsEven/IsOdd package
People not knowing what the modulo operator is
Even if your approach/the switch approach was faster, it makes literally 0 sense to do anything but a modulo operator
But please have fun scaling your if-else statements to support up to 10 digits when it's asked, if not more
as I've pointed out, a good switch statement solution would contain the modulu operator too, just like an ifelse or a ternary operator solution.
we forgot to benchmark one more thing
TheRanger#3357
REPL Result: Success
Console Output
Compile: 646.896ms | Execution: 89.763ms | React with ❌ to remove this embed.
The requirement was clear, we only care about single digits. I think if the switch would be faster and performance really really really matters, then it would make sense.
bitwise should be faster
but release mode optimizes code anyway
Huh
Even then
Your attempting to cut corners to make it easier but it just makes it worse
Matuda#8863
I solved the problems
Quoted by
<@!689473681302224947> from #✅ If else statement problem (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View