Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by thieum · Sep 22, 2012 at 11:04 PM · c#syntax

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 ?:)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

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
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thieum · Sep 23, 2012 at 08:30 AM 0
Share

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?

avatar image aldonaletto · Sep 23, 2012 at 06:18 PM 0
Share

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

avatar image
-1

Answer by T27M · Sep 22, 2012 at 11:11 PM

Your Answer

Stack Overflow is better for none unity questions.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thieum · Sep 22, 2012 at 11:19 PM 1
Share

mmm, I prefer to keep in warm company of Unity users!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges