Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Conditional (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====C-like languages==== [[C (programming language)|C]] and C-like languages have a special [[ternary operator]] ([[?:]]) for conditional expressions with a function that may be described by a template like this: <code>condition ? evaluated-when-true : evaluated-when-false</code> This means that it can be inlined into expressions, unlike if-statements, in C-like languages: <syntaxhighlight lang="c"> my_variable = x > 10 ? "foo" : "bar"; // In C-like languages </syntaxhighlight> which can be compared to the Algol-family if–then–else ''expressions'' (in contrast to a ''statement'') (and similar in Ruby and Scala, among others). To accomplish the same using an if-statement, this would take more than one line of code (under typical layout conventions), and require mentioning "my_variable" twice: <syntaxhighlight lang="c"> if (x > 10) my_variable = "foo"; else my_variable = "bar"; </syntaxhighlight> Some argue that the explicit if/then statement is easier to read and that it may compile to more efficient code than the ternary operator,<ref>{{cite web|url=http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/ |title=Efficient C Tips #6 – Don't use the ternary operator « Stack Overflow |publisher=Embeddedgurus.com |date=2009-02-18 |access-date=2012-09-07}}</ref> while others argue that concise expressions are easier to read than statements spread over several lines containing repetition.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)