What is the name of this "technique"? [duplicate]

-1
int valorNo = (p->left == NULL && p->right == NULL) ? p->key : 0;

I saw this in an answer here in the stack and I have no idea how this works, much less how to "read" it.

    
asked by anonymous 04.12.2017 / 19:34

1 answer

1

This is a Ternary Operator. Basically, it's the same thing to do:

if (p->left == NULL && p->right == NULL)
{
    int valorNo = p->key;
}
else
{
    int valorNo = 0;
}

Being structured as follows:

(CONDIÇÃO) ? OPERAÇÃO VERDADEIRA : OPERAÇÃO FALSA;

This is a simplified way to create conditions in the code.

    
04.12.2017 / 19:42