Header Ads

Decision Making - Objective C

Decision making structure require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally,other statements to be executed if the condition is determined to be false.

Following is the general form of a typical decision making structure found in most programming language:

Objective C programming language assumes any non-zero and non-null values are true and if it is either zero or null, then it is assumed as false value.

Objective C programming language provides following types of decision making statements.Click the following links to check their details:

Statement Description
if_statement An if statement consist of a boolean expression followed by one or more statements.
if_else statement An id statement can be followed by an optional else statement,which executes when the boolean expression is false.
nested_if else statement You can use one if or else statement inside another if or else if statement(s).
Switch statement A swu=itch statement allows a variable to be tested for equality against a list of values.
nested Switch statement You can use one switch statement inside another switch statement(s).


The ? : Operator

We have covered conditional operator ? : in previous chapter which can be used to replace if....else statements. It has the following general form:

Exp1 ? Exp 2 : Exp3;


Where Exp1.Exp2 and Exp3 are expressions. Notice that use and placement of the colon.
 The value of a ? expression is determined like this:Exp1 is evaluated. If  it is true,then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its values becomes the value of expression.


If statement
  
If statement consist of a boolean expression followed by one or more statements.

Syntax

if(boolean_expression)
{
       //statements(s);
}

If the boolean expression evaluates to true, then the block of code inside the if statement will be executed. If boolean expression evaluates to false, then the first of code after the end of the code if statement will be executed.

Objective C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

Example

#import<Foundation/Foundation.h>
int main()
{
       int a =10;
       if( a <20)
       {
           NSLog(@"a is less than 20 \n");
       }
       NSLog(@"value of a is:%d\n", a);
       return 0;
}


If...else statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

if(boolean_expression)
{
         statement(s);
}
else
{
         statement(s);
}

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

Objective C programming language assumes any non-zero and non null values as true and if it is either zero or null, then it is assumed as false value.

Example
#import<Foundation/Foundation.h>
int main()
{
       int a =100;
       if( a <20)
       {
           NSLog(@"a is less than 20 \n");
       }
       else
       {
           NSLog(@"a is not less than 20\n");
       }
       NSLog(@"value of a is:%d\n", a);
       return 0;
}


The if....else if....else Statement

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using if, else if, else statements there are few points to keep in mind:
  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax

if(boolean_expression)
{
         statement(s);
}
else if(boolean_expression)
{
         statement(s);
}
else if(boolean_expression)
{
          statement(s);
}
else
{
          statement(s);
}



Example

#import<Foundation/Foundation.h>
int main()
{
       int a =100;
       if( a == 10)
       {
           NSLog(@"value of  a is 10 \n");
       }
       else if( a == 20)
       {
           NSLog(@"value of a is  20\n");
       }
       else if( a == 30)
       {
           NSLog(@"value of a is  30\n");
       }
       else
       {
            NSLog(@"None of the values is matching \n");
       }
       NSLog(@"Exact value of a is:%d\n", a);
       return 0;
}


Nested if statements

It is always legal in Objective C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statements(s).

Syntax

if(boolean_expression1)
{
        if(boolean_expression2)
        {
         statement(s);
        }

 }

You can nest else if...else in the similar way as you have nested if statement.

Example

#import<Foundation/Foundation.h>
int main()
{
       int a =100;
       int b = 200;
       if( a == 100)
       {
           if( b == 200)
           {
                     NSLog(@"value of  a is 100 and b is 200 \n");
            }
           NSLog(@"value of a is :%d",  a);
      
           NSLog(@"value of b is :%d",b);
       }
       return 0;
}

No comments:

Powered by Blogger.