Forum   Search   Register   Log in SUBADAR FORA
 

Objective-C

Pages: [1]
Programming Help
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 • 2002
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)?
    (Edited 2011 Mar 17 at 13:13)     2011 Mar 17 at 13:12
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5328
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.
Everyone stares when you walk in the room, they stare when you go....
    2011 Mar 17 at 13:45
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 • 2002
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?
    (Edited 2011 Mar 17 at 14:18)     2011 Mar 17 at 14:10
superjer
superjer

2005 Mar 20 • 3767
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.
    (Edited 2011 Mar 17 at 14:17)     2011 Mar 17 at 14:13
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 • 2002
404 ₧
Okay, cool. Thanks alot.
    2011 Mar 17 at 14:19

Pages: [1]
Forum and design copyright © 2008-2010 SuperJer.com