Question Mark and Colon in Statement. What Does It Mean

Question mark and colon in statement. What does it mean?

This is the conditional operator expression.

(condition) ? [true path] : [false path];

For example

 string value = someBooleanExpression ? "Alpha" : "Beta";

So if the boolean expression is true, value will hold "Alpha", otherwise, it holds "Beta".

For a common pitfall that people fall into, see this question in the C# tag wiki.

What does the question mark and the colon (?: ternary operator) mean in objective-c?

This is the C ternary operator (Objective-C is a superset of C):

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

is semantically equivalent to

if(inPseudoEditMode) {
label.frame = kLabelIndentedRect;
} else {
label.frame = kLabelRect;
}

The ternary with no first element (e.g. variable ?: anotherVariable) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar

What does the colon : and the question mark ? operators do?

This is a ternary operator the way it works is :

   condition ? (things to do if true) : (things to do if false);

In your code what it does is :

if value of  Math.random() - 0.5 < 0 
then assign change a values of -5
else
assign change a value of 5.

What is a Question Mark ? and Colon : Operator Used for?

This is the ternary conditional operator, which can be used anywhere, not just the print statement. It's sometimes just called "the ternary operator", but it's not the only ternary operator, just the most common one.

Here's a good example from Wikipedia demonstrating how it works:

A traditional if-else construct in C, Java and JavaScript is written:

if (a > b) {
result = x;
} else {
result = y;
}

This can be rewritten as the following statement:

result = a > b ? x : y;

Basically it takes the form:

boolean statement ? true result : false result;

So if the boolean statement is true, you get the first part, and if it's false you get the second one.

Try these if that still doesn't make sense:

System.out.println(true ? "true!" : "false.");
System.out.println(false ? "true!" : "false.");

PHP syntax question: What does the question mark and colon mean?

This is the PHP ternary operator (also known as a conditional operator) - if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

Think of it as an "if" statement you can use in expressions. Can be very useful in making concise assignments that depend on some condition, e.g.

$param = isset($_GET['param']) ? $_GET['param'] : 'default';

There's also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

$result = $x ?: 'default';

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset or a null coalescing operator which is introduced in PHP7:

$param = $_GET['param'] ?? 'default';

php - Meaning of question mark colon operator

It is a shorthand for an if statement.

$username = $_COOKIE['user'] ?: getusername($_COOKIE['user']);

Is the same as

if( $_COOKIE['user'] ) 
{
$username = $_COOKIE['user'];
}
else
{
$username = getusername($_COOKIE['user']);
}

see test suite here: https://3v4l.org/6XMc4

But in this example, the function 'getusername' probably doesn't work correct, because it hits the else only when $_COOKIE['user'] is empty. So, the parameter inside getusername() is also kind of empty.

What does the question mark as well as the colon (:) do in php, particularly in this line?

This is called a ternary operator. It is presented in this form :

condition ? value_if_condition_true : value_if_condition_false;

For example you can use the result of this expression for an assignment, let's say :

$load_page = is_user_logged_in() ? true : false;

In fact, this is the equivalent of writing :

 if (is_user_logged_in())
$load_page = true;
else
$load_page = false;

EDIT Because I love the ternary operator, I want to write more.
This operator form has no effect on performance or behaviour compared to the classic else/if format, and only serves the purpose of being an elegant one liner. (Which is the reason why some languages don't implement it, taking the view that it is unnecessary and often bad practice to have N ways of writing the same instruction)

But to show how elegant it is, imagine you know the age of a user, and the age_of_subscription to your website, and you want to display a message depending on these two variables according to these rules :

  • if the user is older than 20 and had a subscription for more than 3 year, he is allowed to a discount
  • if the user is younger than 20 and had a subscription for more than 1 years, he is allowed to a discount
  • in any other case, the user is paying the full price.

In the classic IF/ELSE form, you would write :

if ($age > 20)
{
$text = "You are above 20 and you have been a user for ";
if ($age_of_subscription > 3)
{
$text .= "more than 3 years so you are allowed a discount";
}
else
{
$text .= "less than 3 years so you are paying full price";
}

}
else
{
$text = "You are below or 20 and you have been a user for ";
if ($age_of_subscription > 1)
{
$text .= "more than 1 year so you are allowed a discount";
}
else
{
$text .= "less than 1 year so you are paying full price";
}

}
echo $text;

But now, watch the beauty of ternary operator :

echo 'You are ',
($age > 20 ?
($age_of_subscription > 3 ? 'above 20 and you have been a user for more than 3 years
\ so you are allowed a discount' : 'above 20 and you have been a user for less than 3 years
\ so you are paying full price.')
: ($age_of_subscription > 1 ? 'below or 20 and you have been a user for more than 1 year
\ so you are allowed a discount' : 'below or 20 and you have been a user for less than 1 year
\ so you are paying full price.')); // pretty uh?

Question mark and colon in JavaScript

It is called the Conditional Operator (which is a ternary operator).

It has the form of: condition ? value-if-true : value-if-false
Think of the ? as "then" and : as "else".

Your code is equivalent to

if (max != 0)
hsb.s = 255 * delta / max;
else
hsb.s = 0;

question mark and colon - if else in ruby

This is your code, rearranged for easier understanding.

def sort_column
cond = Product.column_names.include?(params[:sort])
cond ? params[:sort] : "name"
# it's equivalent to this
# if cond
# params[:sort]
# else
# 'name'
# end
end

First question mark is part of a method name, the second one - part of ternary operator (which you should read about).



Related Topics



Leave a reply



Submit