Tuesday, February 26, 2013

If.....Else...if.

If you have done any programming, you must have used conditional coding.

The basic conditional codes include:
1. "if" statement
2. "Switch" statement.

int a = 20;

if ( a == 20 )
{
     NSLog ( @" Value is %i :" , a );
}

This is a classic "if" statement used to provide a choice based on the condition that is between the parenthesis after the word "if".

This small piece of code remains same in Objective-C as well.

"else" can also be used in the same manner as you might have known them in any other language.

Example:

if ( a != 20 )
{
     NSLog ( @ "This is not 20" );
}
else
{
    NSLog ( @ "This is 20" );
}


The result will be: This is 20

You can nest multiple "if" satements like you have done before.

if ( condition 1 )
{
   // something happens here
}
else
{
    if ( condition 2 )
  {
     //some code comes here
  }
}

NOTE:
Do remember to take care of your curly braces "}".
If you open a curly brace, always close one.
Xcode will show an error if you don't.

No comments:

Post a Comment