Ternary-if Converter 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&lt;0)s="Neg";else if(i&gt;0)s="Pos";else s="Neut";


Can be shortened with a ternary-if to:

s=i&lt;0?"Neg":i&gt;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 &gt;&lt;(){}[];?:!&amp;|, 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 =&lt;&gt;!+-/*%&amp;|[], 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&lt;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&amp;&amp;b)r=0;else if(a&amp;&amp;!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).


General rules:


This is 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 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 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&lt;0)s="Neg";else if(i&gt;0)s="Pos";else s="Neut";
Output:  s=i&lt;0?"Neg":i&gt;0?"Pos":"Neut";

Input:   if(i%2&lt;1)r=10;else r=20;
Output:  r=i%2&lt;1?10:20;

Input:   if(n&lt;10)if(m&lt;0)i=0;else i=10;else if(m&lt;0)i=-1;else i=1;
Output:  i=n&lt;10?m&lt;0?0:10:m&lt;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&amp;&amp;b)r=0;else if(a&amp;&amp;!b)r=1;else r=-1;
Output:  r=a&amp;&amp;b?0:a&amp;&amp;!b?1:-1;

Input:   if(i[0]&gt;0)if(j&gt;0)if(q&gt;0)r="q";else r="j";else r="i";else r="other";
Output:  r=i[0]&gt;0?j&gt;0?q&gt;0?"q":"j":"i":"other";

Input:   if(i&gt;0)r="i";else if(j&gt;0)r="j";else if(q&gt;0)r="q";else r="other";
Output:  r=i&gt;0?"i":j&gt;0?"j":q&gt;0?"q":"other";

Input:   if(a&gt;0)if(a&lt;2)x="one";else if(a&lt;3)x="two";else if(a&lt;4)x="three";else x="other";else x="other";
Output:  x=a&gt;0?a&lt;2?"one":a&lt;3?"two":a&lt;4?"three":"other":"other";

Input:   if(b[0]&lt;=b[1])q=5;else if(b[0]==null)q=0;else q=-10;
Output:  q=b[0]&lt;=b[1]?5:b[0]==null?0:-10;
Python 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!



Saved:


-1 byte, thanks to Kevin Cruijssen