Using the ternary operator

Ternary operator from expressions and conditional operator: the condition? Expression 1: Expression 2;

if-else re-assignment is the first operation, in order to save time, branch prediction will first guess if or else run and continue to run (the default is if), if guess due to parallel computing and save time, but if the wrong operation due to elimination waste time.
Parallel operation is stopped when the three head assignment is the first re-operation, and branch selection judgment condition encountered.

Although three head operation can only use two expressions, but also ternary operator and if-else, as can be nested statements.

In most cases, but generally, the calculation result is possible as close to 0 and 1, the branch prediction parallel operation & consuming more time than three head operation, the use of more simple calculation using the three head quickly.

Of course, three head operation is a plurality of values can be compared, for example, compare the size, and the three head calculation if there is a better comparison.
For example: if the comparison using the size and the ternary operator to compare the three inputs of integer parameters, and sorted in ascending

If only output after the output conditions are finished.
Here Insert Picture Description
Here Insert Picture Description

Used if it is only able to determine whether such a cumbersome step of nesting is circulated Analyzing

However, as this type of expression is determined using ternary operator symbol to be calculated relatively simple.
As used herein, ternary operator performs magnitude comparison value is determined, then compared again according to the acquired parameter value to compare
Here Insert Picture Description

Sufficient known from the example of using a three-head operation, the extent of the code profile is better than the readability than if-else if-else. However, ternary operator can perform only simple arithmetic determination.

Guess you like

Origin blog.csdn.net/qq_39686486/article/details/89361295