jwarner112
jwarner112

Reputation: 1502

Could someone explain ternary operators in plain English or pseudocode?

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!

Upvotes: 1

Views: 1265

Answers (8)

Soner Gönül
Soner Gönül

Reputation: 98810

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;

Upvotes: 1

Daniel
Daniel

Reputation: 4946

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]

Upvotes: 1

lesderid
lesderid

Reputation: 3430

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();

Upvotes: 1

Andrew
Andrew

Reputation: 2335

Basically

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

Upvotes: 1

Jonesopolis
Jonesopolis

Reputation: 25370

it can be rewritten as:

string path;

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

Upvotes: 1

Cyral
Cyral

Reputation: 14153

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];
}

Upvotes: 4

System Down
System Down

Reputation: 6270

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

Upvotes: 5

Kamil Budziewski
Kamil Budziewski

Reputation: 23107

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

Upvotes: 1

Related Questions