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.");
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 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 is the Java ?: operator called and what does it do?
Yes, it is a shorthand form of
int count;
if (isHere)
count = getHereCount(index);
else
count = getAwayCount(index);
It's called the conditional operator. Many people (erroneously) call it the ternary operator, because it's the only ternary (three-argument) operator in Java, C, C++, and probably many other languages. But theoretically there could be another ternary operator, whereas there can only be one conditional operator.
The official name is given in the Java Language Specification:
§15.25 Conditional Operator ? :
The conditional operator
? :
uses the boolean value of one expression to decide which of two other expressions should be evaluated.
Note that both branches must lead to methods with return values:
It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
In fact, by the grammar of expression statements (§14.8), it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.
So, if doSomething()
and doSomethingElse()
are void methods, you cannot compress this:
if (someBool)
doSomething();
else
doSomethingElse();
into this:
someBool ? doSomething() : doSomethingElse();
Simple words:
booleanCondition ? executeThisPartIfBooleanConditionIsTrue : executeThisPartIfBooleanConditionIsFalse
What is the question mark for in a Typescript parameter name
It is to mark the parameter as optional.
- TypeScript handbook https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters
- TypeScript Deep Dive https://basarat.gitbook.io/typescript/type-system/functions#optional-parameters
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 character ('?') mean in C++?
This is commonly referred to as the conditional operator, and when used like this:
condition ? result_if_true : result_if_false
... if the condition
evaluates to true
, the expression evaluates to result_if_true
, otherwise it evaluates to result_if_false
.
It is syntactic sugar, and in this case, it can be replaced with
int qempty()
{
if(f == r)
{
return 1;
}
else
{
return 0;
}
}
Note: Some people refer to ?:
it as "the ternary operator", because it is the only ternary operator (i.e. operator that takes three arguments) in the language they are using.
What does the question mark character ('?') mean?
?
is the first symbol of the ?:
conditional operator.
a = (b==0) ? 1 : 0;
a
will have the value 1 if b
is equal to 0
, and 0 otherwise.
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;
Verilog question mark (?) operator
That's a ternary operator. It's shorthand for an if statement
Format:
condition ? if true : if false
Example:
tone[23] ? clkdivider-1 : clkdivider/2-1
Translates to something like (not correct syntax but I think you'll get it):
if tone[23] is 1, counter = clkdivider-1
else counter = clkdivider/2-1
Here are two examples of a 2 to 1 MUX using if statement and ternary operator.
On the asic-world website, it is covered under Conditional Operators
Related Topics
Memory Address of Variables in Java
Java8 Lambdas VS Anonymous Classes
Differencebetween Method Overloading and Overriding
Can Java 8 Code Be Compiled to Run on Java 7 Jvm
Keyword for the Outer Class from an Anonymous Inner Class
Why Stringbuilder When There Is String
Convert Timestamp in Milliseconds to String Formatted Time in Java
How to Query Xml Using Namespaces in Java with Xpath
Convenient Way to Parse Incoming Multipart/Form-Data Parameters in a Servlet
Inject an Ejb into Jax-Rs (Restful Service)
Why Is the Clone() Method Protected in Java.Lang.Object
Creating Multiple Log Files of Different Content with Log4J
Convert HTML Character Back to Text Using Java Standard Library