if-else 和自我提问

发布于 2024-11-27 08:55:34 字数 641 浏览 1 评论 0原文

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 文件中。 characterself(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 ifinstead 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清浅ˋ旧时光 2024-12-04 08:55:34

问题是,如果你有 3 个 if 语句,所有这些条件都会被检查,因为年龄只能有 1 个值,你应该使用

if (age == 2)  //You need the double equals, to check for equality, if you use one equals it will assign the value and the if statement will always be true.

else if (age == 3)

else if (age == 4)

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

if (age == 2)  //You need the double equals, to check for equality, if you use one equals it will assign the value and the if statement will always be true.

else if (age == 3)

else if (age == 4)

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 as retain it will increase the retain count by 1, if you assign it without self it wont increase the retain count. Although since you are assigning it a primitive it doesn't matter in this case, only objects have a retain count.

黯然 2024-12-04 08:55:34

您的第一个片段将始终记录 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 with elses could skip some. (Also consider the switch 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.

时光倒影 2024-12-04 08:55:34

它们是两个不同的东西。如果您有一个 if 语句,后跟 4 个 else if,则只会执行这 4 个条件中的一个。例如,如果您有

     int age = 2;

 if (age == 2){
     NSLog(@"2");
 }else if (age <=2 ){
    NSLog(@"<=2");
 }else if (age >=2 ){
        NSLog(@">=2");
 }

上述代码的结果将是 2。这是因为第一个条件为真,所以我们不需要检查其他条件。但是,如果您正在处理一堆“if”,那么

    int age = 2;

if (age == 2){
    NSLog(@"2");
}
if (age <=2 ){
   NSLog(@"<=2");
}
if (age >=2 ){
       NSLog(@">=2");
}

结果将是 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

     int age = 2;

 if (age == 2){
     NSLog(@"2");
 }else if (age <=2 ){
    NSLog(@"<=2");
 }else if (age >=2 ){
        NSLog(@">=2");
 }

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, say

    int age = 2;

if (age == 2){
    NSLog(@"2");
}
if (age <=2 ){
   NSLog(@"<=2");
}
if (age >=2 ){
       NSLog(@">=2");
}

The result of that would be 2 <=2 >=2. In essence all statements were executed.

停顿的约定 2024-12-04 08:55:34

在这种情况下,if 和 else if 没有区别。由于条件是排他性的,请考虑以下示例:

if(age > 15)
{ 
    NSLog(@"over 15");
}

if (age < 17)
{
    NSLog(@"under 17");
}

这将打印
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:

if(age > 15)
{ 
    NSLog(@"over 15");
}

if (age < 17)
{
    NSLog(@"under 17");
}

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.

两相知 2024-12-04 08:55:34

流程控制风格是许多激烈的准宗教争论的主题;)

在必须在包罗万象之前捕获有限数量的情况的情况下,您通常会使用else if否则 情况。假设您想检查年龄是 3 岁、4 岁还是其他任何值:

if (age == 3) {
} else if (age == 4) {
} else {
}

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-all else case. Say if you wanted to check if age was 3, 4 or anything else else:

if (age == 3) {
} else if (age == 4) {
} else {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文