+ 1

Rewrite the code using ternary operators

if(i%2==0) sum=sum-(int)k; else sum=sum+(int)k;

27th May 2021, 1:30 PM
Neeta Maddheshiya
Neeta Maddheshiya - avatar
18 ответов
+ 3
no all languages have ternary operator, and you don't provide the targeted one ^^ anyway, for languages that support it, ternary syntax commonly look like: sum = i%2==0 ? sum-k : sum+k for those wich doesn't support it, often you could inline if..else in the right hand side of an assignement (but syntax sligthly differ depending on language)
27th May 2021, 1:38 PM
visph
visph - avatar
+ 3
visph pseudocode but I think he wants python? or c?
27th May 2021, 1:50 PM
Oma Falk
Oma Falk - avatar
+ 2
you have a language for us? sum - = int(k) if i%2==0 else sum+=int(k)
27th May 2021, 1:38 PM
Oma Falk
Oma Falk - avatar
+ 2
value_if_true if condition else value_if_false https://code.sololearn.com/clENZKjrst6t/?ref=app
27th May 2021, 1:49 PM
Oma Falk
Oma Falk - avatar
+ 2
visph kotlin has none? That's suprising! thought it was Java under the hood. You found a reason for it?
27th May 2021, 2:06 PM
Oma Falk
Oma Falk - avatar
+ 2
In java, using ternary operator, sum = i%2==0 ? sum-(int)k : sum+(int)k ;
27th May 2021, 2:12 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 yes, that's what I was thinking, so I trust you easily ;) just I'm not enough aware of Java (nor Kotlin) to answer without doubt (Kotlin wich I seen yesterday that ut doesn't have, surprising me and make me less sure that Javas has one)
27th May 2021, 2:22 PM
visph
visph - avatar
+ 1
Frogged for what language is your example of inlined if..else?
27th May 2021, 1:43 PM
visph
visph - avatar
+ 1
Java
27th May 2021, 1:51 PM
Neeta Maddheshiya
Neeta Maddheshiya - avatar
+ 1
Neeta Maddheshiya I don't know if java has one or not: Kotlin doesn't have (I discovered that yesterday), and instead use: sum = if (i%2==0) sum-k else sum+k (assuming sum and k are of same type)
27th May 2021, 1:55 PM
visph
visph - avatar
+ 1
Frogged the same reason as python: a design choice ^^
27th May 2021, 2:07 PM
visph
visph - avatar
+ 1
Jayakrishna🇮🇳 I was not sure for Java, because I was thinking it has a ternary, but not Kotlin, so that make me doubt ^^
27th May 2021, 2:14 PM
visph
visph - avatar
+ 1
visph Frogged Java has it: https://www.baeldung.com/java-ternary-operator Neeta Maddheshiya See if you can do it by your own after reading about ternary on the site. If not, just ping me. Happy coding!
27th May 2021, 2:16 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
visph Java have ternary operator ?: Am sure, it works.. I don't know about kotlin have it or not ..
27th May 2021, 2:18 PM
Jayakrishna 🇮🇳
0
visph frogged used python
27th May 2021, 1:46 PM
Nor'wester 🌪️ 🇧🇩 (INACTIVE)
Nor'wester 🌪️ 🇧🇩 (INACTIVE) - avatar
0
Nor'wester 🌪️ 🇧🇩 are you sure? because OP code seems not to be python ^^ (or invalid python ;P)
27th May 2021, 1:48 PM
visph
visph - avatar
0
Frogged thanks... I was thinking it was python, but as previously said, I don't think it is the expected language by OP: + no colon at end of if/else statements + no indentation + use of cast by prependind type inside parenthesis
27th May 2021, 1:52 PM
visph
visph - avatar
0
sorry for the typo corrected just after posting ;P
27th May 2021, 1:55 PM
visph
visph - avatar