- Home /
c# syntax ((float>0)?(1):(2))
Hello,
I'm autodidact in c# scripting and I feel more and more enthusiastic about it. I get oftenly stucked with codes that contains this kind of syntax, which I don't totally understand. What's the meaning of the question mark and :( ?
(c# hidden smileys?:)
myValue = (anyFloat>0)?(1f)) :(2f));
or
myValue = (Input.GetKey (KeyCode.Space)? Time.deltaTime: -Time.deltaTime) );
Is it an IF statement or so? What's the benefit of writing it this way?
Thanks for enlighting me ?:)
Answer by aldonaletto · Sep 22, 2012 at 11:49 PM
This is a shortcut for an if:
myValue = anyFloat>0? 1f : 2f;
is the same as:
if (anyFloat > 0){
myValue = 1F;
} else {
myValue = 2F;
}
but is more compact to write, and compiles to a little shorter and faster code:
IL_0001: ldarg.0
IL_0002: ldfld float32 TestIf::anyFloat
IL_0007: ldc.r4 0.0
IL_000c: ble.un IL_001b
IL_0011: ldc.r4 1
IL_0016: br IL_0020
IL_001b: ldc.r4 2
IL_0020: stfld float32 TestIf::myValue
That's how the if alternative is compiled:
IL_0025: ldarg.0
IL_0026: ldfld float32 TestIf::anyFloat
IL_002b: ldc.r4 0.0
IL_0030: ble.un IL_0045
IL_0035: ldarg.0
IL_0036: ldc.r4 1
IL_003b: stfld float32 TestIf::myValue
IL_0040: br IL_0050
IL_0045: ldarg.0
IL_0046: ldc.r4 2
IL_004b: stfld float32 TestIf::myValue
Thank you Aldo for this very usefull and detailled answer! It liberates some neurons in my brain :°) So it's just a shortcut, I expected something more sophisticated. Interesting to learn it's a bit faster, so why don't we meet this expression more often?
I use this in my programs, but usually avoid it in my answers just to keep focus in the main idea - from my experience, cryptic C-style notation is confusing for most programmers, even the most skilled ones! The C language brought several similar shortcuts and innovations that were adopted in almost all modern languages, but extensive use of them makes the program very hard to understand - so hard that an international yearly contest was created to select the most cryptic C program: the IOCCC, International Obfuscated C Code Contest! But it's interesting to learn and understand these outstanding C features - the original The C Program$$anonymous$$g Language is available as an e-book for download in several sites. Some of these features:
$$anonymous$$ultiple assignment:
a = b = c = 10; // assign 10 to a, b and c
Pre-increment/decrement:
if (++a > 5){ // a is incremented before being compared to 5
Post-increment/decrement:
if (a-- < 0){ // a is compared to 0 before being decremented
Result of assignments:
if ((a = b+1) > 0){ // the result of b+1 is assigned to a and compared to 0
Short-circuit evaluation:
if (a!=0 && b/a>1 && c/a<2){ // if a==0, the other expressions aren't executed, avoiding possible errors
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Using var as a placeholder for velocity... why? 2 Answers
Distribute terrain in zones 3 Answers
syntax for 'using' in javascript 1 Answer
Multiple Cars not working 1 Answer