if/else vs ternary operator

后端 未结 4 1576
青春惊慌失措
青春惊慌失措 2020-12-07 03:04

Considering the evaluation time, are following two equivalent?

if(condition1)
{
    //code1
}
else
{
    //code2
}

condition1 ? code

4条回答
  •  臣服心动
    2020-12-07 03:25

    Well ... In the former case, you can have any amount or type (expression vs statement) of code in place of code1 and code2. In the latter case, they must be valid expressions.

提交回复
热议问题