If-else and self question
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
ins开发者_Go百科tead 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!
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.
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 else
s 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.
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.
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.
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 {
}
精彩评论