A detailed explanation of the ternary operator in Java


Original address: http://www.cnblogs.com/jesonjason/p/5023040.html


For some alternative branch structures , simple conditional operators can be used instead. For example:

if(a<b)
    min=a;
else
    min=b;

This can be handled with the following conditional operators

min=(a<b)?a:b;


  where " (a<b)?a:b " is a "conditional expression", which is executed like this:  if a<b is true, the expression takes the value of a, otherwise it takes the value of b.

  The conditional operator consists of two symbols " ? " and " : ", which require three operands, so it is also called a ternary operator, which is the only ternary operator in the C language.

    Its general form is:

        expression1?expression2:expression3;

  Here are a few notes about conditional operators:

    (1)  Usually, expression 1 is a relational expression or logical expression , which is used to describe the condition in the conditional expression, and expression 2 and expression 3 can be constants, variables or expressions . For example:

(x==y)?'Y':'N'
(d=b*b-4*a*c)>=0?sqrt(d):sqrt(-d)
ch=(ch>='A'&&ch<='Z')?(ch+32):ch

        All of the above are valid conditional expressions.

    (2)  The execution order of the conditional expression is: first solve the expression 1, if the value is not 0, it means that the condition is true, then solve the expression 2, at this time the value of the expression 2 is used as the value of the entire conditional expression;

      If the value of expression 1 is 0, indicating that the condition is false, then expression 3 is solved, and the value of expression 3 is the value of the entire conditional expression. For example:

        (a>=0)?a:-a   The result of execution is the absolute value of a.

    (3)  In the program, by directly assigning the value of the conditional expression to a variable. For example:

        min=(a<b)?a:b The execution result is to assign the value of the conditional expression to the variable min, that is, assign the smaller of a and b to min.

    (4)  The precedence of the conditional expression is only higher than the assignment operator, and lower than all the operators encountered before.

        Therefore, min=(a<b)?a:b parentheses can be omitted and can be written directly, min=a<b?a:b If there is x<y?x+1:y-1, it is equivalent to x<y( x+1):(y-1) not equivalent to (x<y?x+1:y)-1

    (5)  The combination direction of the conditional operator is "right to left".

    (6)  Conditional expressions are allowed to be nested, that is, expression 2 and expression 3 in conditional expressions are allowed to be a conditional expression. For example:

        x>0?1:x<0?-1:0

       上述条件表达式中,表达式3部分又是一个条件表达式.根据条件表达式的结合性,上述条件表达式等价于:

          x>0?1:(x<0?-1:0)

       其作用是判断x的符号情况.当x为正数时,该条件表达式的值为1;当x为负数时,该条件表达式的值为-1;当x为0时,该条件表达式的值为0.

    (7) 条件表达式不能取代一般的if语句,仅当if语句中内嵌的语句为赋值语句(且两个分支都给同一变量赋值)时才能代替if语句.例如:

         if(a%2==0)
            printf("even/n");
        else
            printf("odd/n");

      不能写成:

         (a%2==0)?printf("even/n"):printf("odd/n");

      但可以用下面语句代替:

         printf("%s/n",(a%2==0?"even":"odd");

      该语句的作用是:若 a 为偶数,输出 even;若 a 为奇数,输出odd.

     (8) 表达式1,表达式2,表达式3的类型可以不同.此时件表达式的值的类型为它们中较高的类型.例如:

        main() {

          char c1, ch;

          ch = getchar();

          c1 = ch <= 'Z' && ch >= 'A' ? ' @ ' : ch ;

          putchar(c1);

        }

      该程序的作用是从键盘输入任意一个字符,判别它们是否大写字母,如果是,输出一个@;否则按原样输出 dot

      上例中,程序第6行是关键语句,该语句实现输入字符的判断,选择与更换工作.

      其执行过程为:先由此语句中的赋值号右侧的条件运算符对输入的字符进行判断和选择,若ch>='A'&&ch<='Z'成立,说明ch是大写英文字母,此时选@;否则仍选原字符ch,然后把选择的结果赋值给原变量ch 。

************** 分享技术知识,分享快乐喜悦*******************

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326090861&siteId=291194637