Objective-C

Objective-C

molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Okay, I'm trying to get a little into C stuff since it's required for iOS programming. But I don't really get the writing formalities yet. This is probably an easy question, ha.

For example this code:
code
BOOL trueOrFalse = YES;
if (trueOrFalse)
NSLog(@"trueOrFalse is true.")
if (1)
NSLog(@"1 is considered to be true.");

What does if (trueOrFalse) check here? Is it basically like, "if trueOrFalse is yes" or what does the "if" relate to? I mean, it doesn't say "if(trueOrFales == YES)", but instead just the variable. :O

And does the if(1) the same as if(trueOrFalse == 1)?
LET LOVE REIGN
 
 
 
2011 Mar 17 at 20:12 UTC — Ed. 2011 Mar 17 at 20:13 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
It's the same as many languages. Think about what if(true) or if(false) might mean - basically, that is the end comparison that is made. Say you have
code
if(expr_1 && expr_2) { doSomething(); }

For argument's sake say expr_1 is true and expr_2 is false. You then get
code
if(true && false)

which is
code
if(false)

which means the if statement won't execute. Now, there are exceptions and modifications to this, but this is the basic way to think about it. While you can add if(something == true) to an if block there is no particular need but that can aid readability and help catch errors quickly.

This is why I never do the latter, I love errors.
 
 
 
2011 Mar 17 at 20:45 UTC
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Okay.

So if I have
code
if(variable){
party()
}

it basically does check if the variable is true and then does something, so it's the same as if(variable == true)?

And would
code
if(!variable){
party()
}

check if the variable is false? Or can I only use !s with =s?
LET LOVE REIGN
 
 
 
2011 Mar 17 at 21:10 UTC — Ed. 2011 Mar 17 at 21:18 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Think of everything in terms of expressions. An expression is either true or false. When you give an if(...) a true expression, it will succeed.

!variable will do exactly what you want.

If variable is true, then !variable is false and vice versa.
 
 
 
2011 Mar 17 at 21:13 UTC — Ed. 2011 Mar 17 at 21:17 UTC
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Okay, cool. Thanks alot.
LET LOVE REIGN
 
 
 
2011 Mar 17 at 21:19 UTC
Page [1]