if-else 和自我提问
int age == 3;
if (age == 2)
NSLog(@"2");
if (age == 4)
NSLog(@"4");
if (age == 3)
NSLog(@"3");
我想知道放置 else if
而不是三个 if 语句有什么意义。
for (GameObject *character in listOfGameObjects) {
if (character.myGameObjectType == kPowerUpTypeHealth) {
characterHealth = 100.0f;
}
}
这位于 Viking.m 文件中。 character
和 self
(viking) 都有实例变量、setter 和 getter 方法 characterHealth
。如果我想让 Viking 的生命值等于 100,我是否必须编写 self.characterHealth = 100.0f;
以确保生命值不会添加到 character
中?或者我已经拥有的很好吗?
谢谢你!
int age == 3;
if (age == 2)
NSLog(@"2");
if (age == 4)
NSLog(@"4");
if (age == 3)
NSLog(@"3");
I was wondering what's the point of putting else if
instead of having three if statements.
for (GameObject *character in listOfGameObjects) {
if (character.myGameObjectType == kPowerUpTypeHealth) {
characterHealth = 100.0f;
}
}
This is in the Viking.m file. character
and self
(viking) both have the instance variable, setter and getter method characterHealth
. If I want to make Viking's health equal to a hundred do I have to write self.characterHealth = 100.0f;
to make sure the health isn't added to character
? Or is what I already have fine?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是,如果你有 3 个 if 语句,所有这些条件都会被检查,因为年龄只能有 1 个值,你应该使用
so 在满足其中一个条件后,其他条件将不会被检查,从而节省处理时间。
关于 self,它会以两种方式工作,但是如果characterHealth是一个@property,使用self将使用@synthetize设置器来对其进行赋值,所以如果你将它声明为
retain
会将retain
计数增加 1,如果您在没有 self 的情况下分配它,则不会增加retain
计数。尽管由于您为其分配了一个primitive
,但在这种情况下并不重要,只有objects
具有保留计数。The thing is that if you have 3 if statements, all of those conditions will be checked, since age can only have 1 value, you should use
so after one of those conditions is met, the other conditions will NOT be checked, saving processing time.
Regarding the self, it will work both ways, however if characterHealth is a
@property
, using self will assing it using the@synthetize setter
, so if you have it declared asretain
it will increase theretain
count by 1, if you assign it without self it wont increase theretain
count. Although since you are assigning it aprimitive
it doesn't matter in this case, onlyobjects
have a retain count.您的第一个片段将始终记录 2、4 和 3,因此它与 if/else if/else if 序列(始终记录 2,仅记录 2)非常不同。
如果您使用
if (a == 2)
编写它,那么与 if/else 的区别在于您的代码将始终运行这三个测试,而使用else
的代码将始终运行这三个测试code>s 可以跳过一些。 (另请考虑switch
语句。)对于第二个问题,省略
self
是安全的。但如果你觉得它模棱两可/令人困惑,那就把它写出来。它不会改变任何东西。Your first snippet will always log 2, 4 and 3, so it's very different from an if/else if/else if sequence (which would always log 2, and nothing else).
If you had written it with
if (a == 2)
, then the difference with an if/else is that your code will always run the three tests, while the one withelse
s could skip some. (Also consider theswitch
statement.)For your second question, it's safe to omit the
self
. But if it looks ambiguous/confusing to you, do put it. It doesn't change anything.它们是两个不同的东西。如果您有一个 if 语句,后跟 4 个 else if,则只会执行这 4 个条件中的一个。例如,如果您有
上述代码的结果将是
2
。这是因为第一个条件为真,所以我们不需要检查其他条件。但是,如果您正在处理一堆“if”,那么结果将是
2 <=2 >=2
。本质上所有语句都被执行了。They are 2 different things. If you have an if statement followed by 4 else if's then only one out of those 4 conditions would be executed. For instance if you have
The result of the above code would be
2
. This is because the first condition was true so we dont need to check others. However if you are dealing with a bunch of if's, sayThe result of that would be
2 <=2 >=2
. In essence all statements were executed.在这种情况下,if 和 else if 没有区别。由于条件是排他性的,请考虑以下示例:
这将打印
15岁以上
under 17
使用 else if 仅打印其中之一。
当访问属性时,假定为 self。它不会改变字符,因为您没有指定character.characterHealth。
In this case if and else if don't make a difference. Since the conditions are exclusive consider this example:
This will print
over 15
under 17
Use else if to only print one of these.
When accessing a property self is assumed. It wouldn't change character because you haven't specified character.characterHealth.
流程控制风格是许多激烈的准宗教争论的主题;)
在必须在包罗万象之前捕获有限数量的情况的情况下,您通常会使用
else if
否则 情况。假设您想检查年龄是 3 岁、4 岁还是其他任何值:Flow control style is the subject of many heated quasi-religious arguments ;)
You'd normally use
else if
in a situation where you have to catch a finite number of cases before a catch-allelse
case. Say if you wanted to check if age was 3, 4 or anything else else: