Could someone explain ternary operators in plain English or pseudocode?

夙愿已清 提交于 2019-12-13 00:44:53

问题


I don't understand the syntax used in the following lines, except that it follows a basic structure of what seems to be called a ternary operator.

string path = args == null || args.Length == 0 ?
    @"C:\GENERIC\SYSTEM\PATH" :
    args[1];

I'm new to this syntax. Would someone help me translate it into real English (or pseudocode), much in the way an if statement can be turned into "if this then that"?

EDIT: Thank you everyone for your answers, you've all been extremely helpful. Unfortunately I can only vote one of you, but I'll upvote a bunch of you!


回答1:


What you are seeing is a special conditional operator, the ternary operator. (And here is a nice tutorial)

It is used like so:

condition ? first_expression : second_expression;

Basically if the statement is true, the first expression is executed, if not, the second is. Generally speaking it is a small shortcut for if/else blocks, and should be used for only small statements. Nesting the ternary operator is largely frowned upon.

So if args == null || args.Length == 0 Then path = @"C:\GENERIC\SYSTEM\PATH", if not, it equals args[1]

It is equivalent to your standard if block

string path;
if(args == null || args.Length == 0)
{
   path = @"C:\GENERIC\SYSTEM\PATH";
}
else
{
   path = args[1];
}



回答2:


This is equivalent to

string path;
if(args == null || args.Length == 0)
    path = @"C:\GENERIC\SYSTEM\PATH" ;
else
    path = args[1];

You can break down a ternary operator to this

VariableToStoreResult = BooleanCondition ? ValueIfConditionIsTrue : ValueIfConditionIsFalse



回答3:


string path = "";

if(args==null || args.Length==0)
{
   path = @"C:\GENERIC\SYSTEM\PATH";
}
else
{
   path = args[1];
}

This is a translation. Ternary operator looks like:

result = (condition)?firstResult:otherResult

your ternary operator means: if args are null or empty -> use default path | else -> use path from args




回答4:


it can be rewritten as:

string path;

if(args == null || args.Length == 0)
    path = @"C:\GENERIC\SYSTEM\PATH";
else
    path = args[1];



回答5:


Basically

If args is null or length of args is zero
Then
Path = "C:\Generic\System\Path"
Else
Path = args[1]




回答6:


Like Jon Skeet has said in the comments, this operator is called the conditional operator. The reason behind is name is that it works very much like an if-statement. It's often called the ternary operator, because it's currently the only operator with three operands.

Now, the explanation:

int myInt = myBool ? valueWhenTrue : valueWhenFalse;

This translates into something like:

int myInt;
if(myBool)
   myInt = valueWhenTrue;
else
   myInt = valueWhenFalse;

Important note: The conditional operator can only be used for expressions (and is evaluated itself as an expression), not statements. This, for example, is invalid code:

myBool ? DoSomething() : DoSomethingElse();



回答7:


The structure is quite basic

variable = value;

but now the value depends on a condition that renders true or false;

variable = condition ? true : false;

Condition can be anything, even a function that returns this true or false state.

What you see in the example you submitted is a combined condition.

string path = args == null || args.Length == 0 ?
    @"C:\GENERIC\SYSTEM\PATH" :
    args[1];

Here the conditions renders true if one of the statements in the "OR" is true

read

string path = 

(if "args == null" is true)  OR (if "args.Length == 0" is true) then value = @"C:\GENERIC\SYSTEM\PATH" 
 else
(if both false) then  value = args[1]



回答8:


From high level to low level, here the operators precedence;

==, ||, ?:, =

So basicly, your code equavalent to;

string path;
if((args == null) || (args.Length == 0))
{
    path = @"C:\GENERIC\SYSTEM\PATH" ;
}
else
{
    path = args[1];
}

Take a look at ?: Operator (C# Reference)

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;


来源:https://stackoverflow.com/questions/19144553/could-someone-explain-ternary-operators-in-plain-english-or-pseudocode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!