Reputation: 2883
i saw this in the internet, and i couldn't think what is this about:
they created the boolean which is named obstacles and set it to false.
public bool obstacles = false;
But, what is this line about:
obstacles = obstacles ? false : true;
if (obstacles)
{
transform.renderer.material.color = new Color(.5f, .5f, 0.0f);
}
else
{
transform.renderer.material.color = Color.white;
}
I know the if and else function, when there is an obstacles, turn the material to the assigned color, or if there is not an obstacles, turn the material into white color. But, what is this line about: obstacles = obstacles ? false : true;
Thanks.. Sorry for this newbie question.
Upvotes: 0
Views: 141
Reputation: 172518
?:
This is called the ternary operator
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;
Remarks
The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
The example from MSDN makes it clear:
int input = Convert.ToInt32(Console.ReadLine());
string classify;
// if-else construction.
if (input < 0)
classify = "negative";
else
classify = "positive";
// ?: conditional operator.
classify = (input < 0) ? "negative" : "positive";
As Steeve correctly pointed out, you may try like this using a if else statement to what you want to accomplish:
if (!obstacles)
{
transform.renderer.material.color = new Color(.5f, .5f, 0.0f);
obstacles = true;
}
else
{
transform.renderer.material.color = Color.white;
obstacles = false;
}
Upvotes: 5
Reputation: 216313
All the answers explain what is a ternary operator and they are all right, but everyone fails to note the elephant in the room. Why not to remove the ternary operator and write the code above as
if (!obstacles)
{
// Enter the if with obstacles = false, but execute the code for true then flip obstacles
transform.renderer.material.color = new Color(.5f, .5f, 0.0f);
obstacles = true;
}
else
{
// Enter the else with obstacles = true, but execute the code for false then flip obstacles
transform.renderer.material.color = Color.white;
obstacles = false;
}
Upvotes: 4
Reputation: 26209
?: called ternary operator
functionality :
var value= (Condition/Expression)? value1 :value2;
means
if(Condition== true)
value=value1;
else
value=value2;
Your code:
public static bool obstacles = true;
bool value=(obstacles = obstacles) ? false : true;
output: value=false
your expression => (obstacles = obstacles)
always evaluates to whatever value is there in obstacles
so here it is true
.
hence first value here false
will be assigned to your variable
value
.
Upvotes: 1
Reputation: 554
This line basically "flips" the value of "obstacles" variable to the opposite value. If obstacles is true, it changes it's value to false and vice-versa. So, the author of this code probably is trying to draw something similar to an interleaved pattern here.
Upvotes: 2
Reputation: 6480
This is the ternary operator. Basically, if condition is true, execute first statement, else execute the second.
condition ? first_expression : second_expression;
http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.110).aspx
Upvotes: 1