[Question]
  [
In Java/.NET/C/JavaScript/etc. you can use ternary-ifs to shorten if-statements.
For example (in Java):
```
// there is a String `s` and an int `i`
if(i<0)s="Neg";else if(i>0)s="Pos";else s="Neut";
```
Can be shortened with a ternary-if to:
```
s=i<0?"Neg":i>0?"Pos":"Neut";
```
## Challenge:
**Input:** A regular if-else (possible with nesting) that sets a single variable.
**Output:** The converted ternary-if.
## Challenge rules:
* You can assume all if-else cases are possible without brackets (so each if/else-if/else block has a single body).
* You can assume there won't be any spaces, tabs, or new-lines, except for a single space after each `else` (including at `else if`).
* You can assume the variable names used are always a single lowercase letter (`[a-z]`).
* The values given to the variables can be one of:
	+ Strings (without spaces/tabs/new-lines), which will be surrounded by double-quotes (i.e. `"Test"`, `"SomeString"`, `"Example_string"`, etc.). You can assume the strings will never contain the substrings `if` or `else`, nor will it contain spaces, tabs, newlines, (escaped) double-quotes, or the character `=`. It can contain the characters `><(){}[];?:!&|`, but will be in the printable ASCII range only (`['!' (33), '~' (126)]`).
	+ Integers (i.e. `0`, `123`, `-55`, etc.)
	+ Decimals (i.e. `0.0`, `0.123`, `-55.55`, etc.)
* The values won't ever be mixed. So all variables assigned are integers, and not some are integers and some are strings.
* The conditions within parenthesis can contain the following characters `=<>!+-/*%&|[]`, `a-z`, `0-9`. You can assume there won't be any inner parenthesis, and you can also assume there won't be any (confusing) fields of more than one character used (like `if(if<0)`).
* You can assume there won't be any short-cuts like `i*=10` instead of `i=i*10`.
* You won't have to handle dangling `else` cases, so all `if` can be paired up with an `else`. I.e. `if(a)if(b)r=0;else r=1;` isn't a possible input-case. `if(a)if(b)r=0;else r=1;else r=2;` or `if(a&&b)r=0;else if(a&&!b)r=1;else r=-1;` are however.
* I/O is flexible. Input and Output can be a string, list of characters, read from STDIN, output to STDOUT, etc. Your call.
* All ternaries will have a right associativity, as is the standard in most languages ([but not in for example PHP](https://stackoverflow.com/a/38231137/1682559)).
## General rules:
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer in bytes wins.  
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
* [Standard rules apply](https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422) for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
* [Default Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* If possible, please add a link with a test for your code.
* Also, please add an explanation if possible.
## Test cases:
```
Input:   if(i<0)s="Neg";else if(i>0)s="Pos";else s="Neut";
Output:  s=i<0?"Neg":i>0?"Pos":"Neut";
Input:   if(i%2<1)r=10;else r=20;
Output:  r=i%2<1?10:20;
Input:   if(n<10)if(m<0)i=0;else i=10;else if(m<0)i=-1;else i=1;
Output:  i=n<10?m<0?0:10:m<0?-1:1;
Input:   if(i==1)i=0.0;else i=0.25;
Output:  i=i==1?0.0:0.25;
Input:   if(!a)if(b)r=0;else r=1;else r=2;
Output:  r=!a?b?0:1:2;
Input:   if(a)if(b)r=0;else r=1;else if(c)r=2;else r=3;
Output:  r=a?b?0:1:c?2:3;
Input:   if(a&&b)r=0;else if(a&&!b)r=1;else r=-1;
Output:  r=a&&b?0:a&&!b?1:-1;
Input:   if(i[0]>0)if(j>0)if(q>0)r="q";else r="j";else r="i";else r="other";
Output:  r=i[0]>0?j>0?q>0?"q":"j":"i":"other";
Input:   if(i>0)r="i";else if(j>0)r="j";else if(q>0)r="q";else r="other";
Output:  r=i>0?"i":j>0?"j":q>0?"q":"other";
Input:   if(a>0)if(a<2)x="one";else if(a<3)x="two";else if(a<4)x="three";else x="other";else x="other";
Output:  x=a>0?a<2?"one":a<3?"two":a<4?"three":"other":"other";
Input:   if(b[0]<=b[1])q=5;else if(b[0]==null)q=0;else q=-10;
Output:  q=b[0]<=b[1]?5:b[0]==null?0:-10;
```
  
[Answer]
# [Python 2](https://docs.python.org/2/), ~~126~~ ~~121~~ ~~120~~ ~~114~~ 100 bytes
```
lambda s:findall(' (.=)',s)[0]+sub('if.(.*?)\)(.=)?',r'\1?',sub('.{5} (.=)?',':',s))
from re import*
```
[Try it online!](https://tio.run/##fZPBbuIwEIbvPEWItE3cligO7cWNO@oLVJX2uPQQICmOIAEn1VKt9tnpjJM43WL2gGz@mfn@X4PZf7SbukpOhVycttluuc68RhSqWmfbbRh4YSRZcNuwX/HrTfO@DANVRGF0DWzBqATBrQ4WHA9TjP7c//V6PRA0xyaFrneezj2129e6vT49Pf@UfLLXqmq9gnihSmPWSP85f/Mf8m2Drag9Gu2lbnrNNLy3/kPAJqrwkCIMI2gkzoOZFjgFZkYMzZ3Rv3Y/kpQzLXnckbVM4jOqlqYNeCyo6sBUKY8ZnjtMr2TPUpZqKzNuS2cuShIFsBFigVZ0mXHB3bml5OQUWa84Su4dTGoEbBNd3YGaZpR8iUuwO@B2GY5dTDNYUkKROHEXaSivGCF7ee5gD@gVJGLuxl9dfYF3wpQUm3nGXWAcQ7JpBi5mF7aKL/vR/JBldxzw0NI/@APcL8erGq91u8m173o4hgiIgwO9xwO@xhI/Cj/DkCtI56vG/0DZKeWonGe7nIKs0ZJikL2N8p8IWbeBLE3YEdFVPjpn6Zy09nf9Vbsz2kbnQ@fRJvr29XvAo0QzQCcwPgL5YOh4u4OeOYQdQ58@AQ "Python 2 – Try It Online")
---
Saved:
* -1 byte, thanks to Kevin Cruijssen
[Answer]
# [Perl 5](https://www.perl.org/) -p, ~~50~~ ~~49~~ 48 bytes
```
s/if.(.*?)\)(.=)/\2\1?/g;s/.if./?/g;s/;.{6}=/:/g
```
[Try it online!](https://tio.run/##dY7dCoIwGEDve4oQAhe4Ofu5aPv0DaQH8MaLqQNzshkF0au3dP4URFfbd853xlqh64O1hsgC@3iboAz5GBDJoowmpGSG4N6Q8crw4/gEciKltbLwJQ@RAS8VpcdEbcR6YAAOslRcuwn341kZj63@NbFjbmcOxtwVm4hTpIGGo9QQhU40nIaoPy/9kxImK5e9xQR0Ua7LY5flPEJ38FQjPj/J@W5g3U19s71jlRbz5pB1ldA/40u1nVSNsUH7Bg "Perl 5 – Try It Online")
The 48 byte version is inspired by Neil's Retina answer.
### Explanation
```
# Replace "if(e1)if(e2)x=" with "x=e1)if(e2?"
s/if.(.*?)\)(.=)/\2\1?/g;
# Replace ")if(" with "?"
s/.if./?/g;
# Replace ";else x=" with ":"
s/;.{6}=/:/g
```
### Old 49 byte solution
```
s/.{5} (.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 32 bytes
```
+r`if.(.*?)\)(.=)
$2$1?
;.{6}=
:
```
[Try it online!](https://tio.run/##dZBNTsMwEIX3OQW1ShWDatkpZUE87Q0Q@1KpDnKpo5AoTioqIc4e/FcnArGayffevIlHy17VYhju9UEdSUrutvgVpwRwMs/mbJvk5OvxG5KnYVDHVHGKO0DP8h3lsurkjWUbx16aLjBnOPcoT6x6m3GGNTDqRQ0ZdULNGcWmfphIBUFV0ReVJYuSDwRgdoDEEUqytZNmwgYWZltcxuJW5/jXYPAbtq6AV96@WEzMHswsibHL8FM7ut@455S@tKZoQC26GlE5tmpsm/4kdbiUH1HjZUtPypH8jZ0ECL9a8AxfjFDLcU7wlWX9ZzNlD46dtLw6LzHv16eNL8wbORQ7tsctrGOMxQD1uaoMDpdqzV1ia7of "Retina 0.8.2 – Try It Online") Explanation:
```
r`if.(.*?)\)(.=)
$2$1?
```
Handle an `if` immediately before an assignment by moving the assignment before the condition and appending a `?`. The stage is matched right-to-left to ensure we get the if closest to the assignment, while the `\)` ensures that we don't match `else` by mistake.
```
+
```
Repeat the stage to take care of nested `if`s.
```
;.{6}=
:
```
Any remaining assignments are `else`s so replace the `;else ?=` with a `:`.
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~72~~ 71 bytes
```
->s{$a=$2while s.gsub!(/if.(.*?)\)(.=)?(.*?);\w* (.=)?/,'\1?\3:');$a+s}
```
[Try it online!](https://tio.run/##dZRfk5owFMXf/RTI2IW4yD@3L5GYmX3rdGbtuzidYKPGcRUJjLarn929CQKi2wckOTn3d24kkBXJ38uCXAZj@dFjpBceVmLDDekuZZF0bU8sXNvtUxQj2yWI6vEoPvQNPfUcKw5oPMQWGvXYszxfci7z33MmuSRR9Dp5dWW6EbnR8zo/tmmRY8MwxMIWkY8kMd/40hzxjeRaG2vt105eNW0ocnPUmRR5WSsJVFJdh8FPtRtXtnbEtzAKUEYCv6RlJPRvSBnRBhr4WOmt0m0U@Aju79ClINd6UZPqlUFQL92QBVH1FCzUx4BXg0GAg/v@CAkU3a35vht@b3GUhYIBlyut8i5THSawwXp/Qb3R1j67jCaqExzeIf5LAHmOFOYqD1u8CjenIR7eI5@eboCl0FVK3dsgaMOgAGjaRgM8ePiXpv5srB/Gurzt4ZYRc29WQHPdDEUz3OUrnpntB65ZFEB0r87OHk7OGi4BV2Vvh5dZojmj61JZN8pjP18lqziIUdEqso7/MpaVO2VRiI6A2/ImjUVDpeWH3a32orVVxivnse7ibto0dSQQQyGD6gQMZKq5MHqhV1rVYNMovNGdjlDNkuZNd5cZT22v3AN8IVDPQ@47Sz9O7MSm6hPS92bnzk5nP9Zde3osdElZWAa6/0Rql4zK5CQnmEljamoHjvM4N5@ZY7J5XrDNdb6Ysplj8mPK5zn/g5WUOLZSCUkQtSY/LWy9wa9jWbPz5RM "Ruby – Try It Online")
[Answer]
# [Java (JDK)](http://jdk.java.net/), 119 116 bytes
Almost entirely pure regex solution, chopped and changed a bit from a couple of the other answers.
*-3 bytes thanks to some more regex trickery from Kevin*
```
s->s.replaceAll(".*(.=).*","$1$0").replaceAll("if.(.*?)\\)","$1?").replaceAll("([ ?]).=","$1").replace(";else ",":")
```
[Try it online!](https://tio.run/##lZLfboIwFMbvfYqucYaa0QBuN5Nq9gAzS3YpXlSsWoagbXUui8/OWqgM9y/shnJ@p@c732mb0AN1k8VLEadUSvBIefbeAYBniokljRmYmBCAZyV4tgKxY38kGmp@6uiPVFTxGExABggopDuSWLBtqosf0tSBuO9ggnAf3sCu3/UgusjyJXZwf4yiCJUbxl/yzhSMZwiTMvmZc@CQpZIBje8hKobGx3Y/T7UPa@eQ8wXY6HGs4@kMUGRneZOKbXC@V3irUyrNnAzHxovDQw9JEsEJW0W2haGjij7l8kyrTXulY4jKs/hb9zoIfSSI71XlggReq8Is9D2k1402xomt5rVOnXH9OtXOECG@EcS1pIeDu1alV9QYmutp6mH8eqpWCr8KaBwjo2LxoJ1cr9cQq8CVIbUtt@WhTL3ZqDzupFp2ehH6pnfnazdB0gx4M8jVmom2L8JK88Y7SyxLGuxHC/9pRKtRaBigoynNWEOdhoOSqtf8gt5WdC1YvfvY6PsNWCOnzqn4AA "Java (JDK) – Try It Online")
**Explanation**
```
s->                                         // Lambda function taking a String
   s.replaceAll(".*(.=).*","$1$0")          // Find assigned variable and append to start of String
    .replaceAll("if.(.*?)\\)","$1?")        // Replace any 'if' statements with their condition
                                            // followed by '?'
    .replaceAll("([? ]).=","$1")            // Remove all assignments after a '?' or space
    .replace(";else ",":");                 // Simple replace (no regex) to remove 'else' statements
```
[Answer]
# [Kakoune v2018.09.04](https://kakoune.org/), 43 38 37 bytes
```
xs\w=(?!=)<ret>d<a-h>Psif.<ret>df);r?xs;else<space><ret>c:<esc>
```
Explanation:
Kakoune is a multiple selection based, modal editor, inspired by Vim.
1. `x` select the whole line
2. `s` ... `<ret>` filter the selection with the regular expression `\w=(?!=)`, which matches all variable assignments, and doesn't match `==` comparisons
3. `d` delete each selection and put its contents in the default register
4. `<a-h>` extend all selections to the beginning of their line
5. `P` paste the content of the default register before each selection
6. `s` ... `<ret>` filter the selection with the regular expression `if.`
7. `d` delete each selection
8. `f` extend each selection forward to the next `)`
9. `;` reduce each selection to its cursor
10. `r` replace each character of each selection with `?`
11. `x` select the whole line
12. `s` ... `<ret>` filter the selection with `;else<space>`
13. `c` ... `<esc>` clear each selection and replace it with `:`
### animation of the code on a test case:
[![Code In Action](https://i.stack.imgur.com/fh9tp.gif)](https://i.stack.imgur.com/fh9tp.gif)
[Answer]
# [Clean](https://github.com/Ourous/curated-clean-linux), ~~386~~ ~~375~~ ~~216~~ ~~196~~ 189 bytes
Look ma, no regex!
```
import StdEnv,Data.List
?[_,'=':b]= $b
?b= $b
$['if(':s]#(h,[_:t])=span((<>)')')s
=h++['?': ?t]
$[';else ':s]=[':': ?s]
$[a:b]|b>[]=[a: $b]=b
@s=hd[[v,e: $s]\\['else ',v,e=:'=':_]<-tails s]
```
[Try it online!](https://tio.run/##JY5Ni8IwEIbv/RWBFdJiK@7CXqJjPehhwcOCxxjKpB82kLbiRGFhf/tmE8scZuaB96O2LY5@mJqHbdmAZvRmuE13x86uOY7P/IAOVydDLilllXPgQitgC52U@rUWkpsu5YLUW9rnshJOZUA3HNN0u8t4GEqgXy4lL7lgpVNRsWkttSyKQHIROUWOwftX72SgKIK5Ap3sCfpGymfeBkLqcpF8FucBgYiFKrUtHBpLjJQ/Owzlge3Zq5gBeM8MrFfrOTOeH58brvxf3Vm8ki@@Tv7wM@Jg6vn5tui66T74Qv8D "Clean – Try It Online")
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 80 bytes
```
s=>(p=s.replace(/(?:if.(.*?)\)|;.*? )(.=)?/g,(_,t,v)=>(V=v||V,t)?t+'?':':'),V+p)
```
[Try it online!](https://tio.run/##fZPBUsIwEIbvPoV0RptoW5uil9Cwb@B44qKOU2vAdJBCW5ED746btKSoqcN0Ev7sfv/fJRTZNqvzSq2bcFW@ycNcHGoxJWtRR5VcL7NckhsCXM0jEl0BfaL7Ca7nlESCws0iIC9BE2wptszEdr@fBQ2F5toHn@OHBrPrNT3k5aoulzJalgsyJ76aE5XGtBbevVx4E7ms5bnWpkZ7KOtOMwWfjTfxKZ2cnUL8WiABTD/HPjBd3Jb/rO4sL5KU0UqwuKVXIokd5EqYQmAxN@cu1CplMcX1A99CiY6nLNmehMweOZyU0BzAUog52ulNyDgbyi8E026R9Yuj5M7J1aWAhbyrcOFGmX6DVxyInQezg3HOZZTBq07KkwHkIBHlnGpsJ4@d/CM@h4SPhywuL08MWmGkFZs9ZG44NiLdlAPj4eCUH@Pnqflxi3bZ4FIJb@MdDbyi36p@WzbvsvLcF8owAYGw0Xd1gze1wEfhY9ucYVpv1f9HilYpeuVvvv@SaHu01VF0BBvn3xhZO4ksTegO8SvZu2fpWGvNV3mq3RrtvZLHyp1N9evr35A7gXaAXmCcODqA4ePuFjrqMfBp8MM3 "JavaScript (Node.js) – Try It Online")
Thanks to Kevin Cruijssen, 2 bytes saved.
]