What would be the name of this "technique"?

Asked

Viewed 49 times

-1

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

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

  • What’s that? There’s a lot of stuff in there.

  • 1

    @Matheus, as I understand it, is the ternary operator: Expression of test ? Executed expression_se_true : Executed expression_se_false; Being well explained here.

1 answer

1


That’s a ternary operator. Basically, it’s the same thing as doing:

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;

It’s a simplified way to create conditions in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.