无论如何缩短if(i == x || i == y)? [英] Anyway to shorten if ( i == x || i == y)?

查看:148
本文介绍了无论如何缩短if(i == x || i == y)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图缩短我的代码,来自:

I tried to shorten my code, from :

if(i== x || i == y || i == z )

if (i == ( x || y || z ))

我知道这种方式是错误的,因为我在日志中输入不正确。

I know this way is wrong because I got incorrect i in log.

但是,是否有任何方法可以缩短Objective-C中的代码?

However, is there any method to shorten the code in objective-C ?

推荐答案

如果 x == i 的概率高于 y == i 那么最好把它写成 x == i || y == i 而不是 y == i || x == i 因为如果第一个语句的计算结果为true,则不评估第二个语句(它是短路的)

if there is a higher probability that x==i than y==i then it's better to write it as x==i || y==i as opposed to y==i || x==i because if the first statement evaluates to true, the second one is not evaluated (it's shortcircuited)

这篇关于无论如何缩短if(i == x || i == y)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆