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

369,514

Solution 1

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.");

Solution 2

Thats an if/else statement equilavent to

if(row % 2 == 1){
  System.out.print("<");
}else{
  System.out.print("\r>");
}

Solution 3

a=1;
b=2;

x=3;
y=4;

answer = a > b ? x : y;

answer=4 since the condition is false it takes y value.

A question mark (?)
. The value to use if the condition is true

A colon (:)
. The value to use if the condition is false

Solution 4

Also just though I'd post the answer to another related question I had,

a = x ? : y;

Is equivalent to:

a = x ? x : y;

If x is false or null then the value of y is taken.

Solution 5

Maybe It can be perfect example for Android, For example:

void setWaitScreen(boolean set) {
    findViewById(R.id.screen_main).setVisibility(
            set ? View.GONE : View.VISIBLE);
    findViewById(R.id.screen_wait).setVisibility(
            set ? View.VISIBLE : View.GONE);
}
Share:
369,514
Deepend
Author by

Deepend

Beginner programmer

Updated on July 08, 2022

Comments

  • Deepend
    Deepend almost 2 years

    Two questions about using a question mark "?" and colon ":" operator within the parentheses of a print function: What do they do? Also, does anyone know the standard term for them or where I can find more information on their use? I've read that they are similar to an 'if' 'else' statement.

    int row = 10;
    int column;
    while (row >= 1)
    {
        column = 1;
        while(column <= 10)
        {
            System.out.print(row % 2 == 1 ? "<" : "\r>");
            ++column;
        }
        --row;
        System.out.println();
    }
    
  • Reinstate Monica -- notmaynard
    Reinstate Monica -- notmaynard over 10 years
    What language? Not in Java.
  • Nikola Yovchev
    Nikola Yovchev over 10 years
    That can be but not in java.
  • moo moo
    moo moo about 10 years
    I am working in Java and yes this does work.
  • uyuyuy99
    uyuyuy99 about 10 years
    Sorry, but this does not work in Java. You sure you're not thinking about, say, PHP?
  • Sheepy
    Sheepy almost 10 years
    Ternary operator refers to any operator with three parameters, thus this is a ternary operator but not the ternary operator. Major languages (C#, Java, PHP) consider it a conditional operator, and call it the ?: operator. Occasionally (JavaScript) it is called the conditional operator.
  • Brendan Long
    Brendan Long almost 10 years
    @Sheepy Thanks, I updated the answer.
  • Cemafor
    Cemafor over 9 years
    Groovy has the "Elvis operator" (?:) that behaves like this, similar to C#'s null coalescing operator (??).
  • Cleb
    Cleb over 5 years
    Is there any difference regarding performance? I personally find the classical version far easier to read - especially when x and y are again function calls.
  • Brendan Long
    Brendan Long over 5 years
    @Cleb Theoretically there's no difference, but compilers may optimize the two statements differently (making different assumptions about branch prediction). Most people don't need to worry about that level of optimization, and if you do, you probably want to use attributes like __builtin_expect anyway.
  • Cody
    Cody almost 5 years
    To me, your answer was the only one that made sense based on how you laid it out. So thank you and I have republished your answer with a citation. garrett.ms/2019/07/24/…
  • Menai Ala Eddine - Aladdin
    Menai Ala Eddine - Aladdin over 4 years
    Is it possible to call function after 7 or 8 ? for example if a>7 type 7 then execute other function.
  • user unknown
    user unknown over 4 years
    Do you mean "print 7" with "type 7"? Why don't you try it out? The Java jargon uses the expression method, not functions - in other contexts there is the distinction of function and procedure, dependent on the existence of a return type, which is required in a ternary expression. Afaik, you can call a function which, after printing, returns a value.