Objective-C - declaração if ... else

A if declaração pode ser seguida por um opcional else instrução, que é executada quando a expressão booleana é falsa.

Sintaxe

A sintaxe de um if...else declaração na linguagem de programação Objective-C é -

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
} else {
  /* statement(s) will execute if the boolean expression is false */
}

Se a expressão booleana for avaliada como true, então o if block do código será executado, caso contrário else block de código será executado.

A linguagem de programação Objective-C assume qualquer non-zero e non-null valores como true, e se for zero ou null, então é assumido como false valor.

Diagrama de fluxo

Exemplo

#import <Foundation/Foundation.h>
 
int main () {
   /* local variable definition */
   int a = 100;
 
   /* check the boolean condition */
   if( a < 20 ) {
      /* if condition is true then print the following */
      NSLog(@"a is less than 20\n" );
   } else {
      /* if condition is false then print the following */
      NSLog(@"a is not less than 20\n" );
   }
   
   NSLog(@"value of a is : %d\n", a);
   return 0;
}

Quando o código acima é compilado e executado, ele produz o seguinte resultado -

2013-09-07 22:04:10.199 demo[3537] a is not less than 20
2013-09-07 22:04:10.200 demo[3537] value of a is : 100

A instrução if ... else if ... else

A if declaração pode ser seguida por um opcional else if...else , que é muito útil para testar várias condições usando uma única instrução if ... else if.

Ao usar instruções if, else if, else existem alguns pontos a serem considerados -

  • Um if pode ter zero ou mais um e deve vir depois de qualquer outro if.

  • Um if pode ter zero a muitos else if's e eles devem vir antes do else.

  • Assim que um else if for bem-sucedido, nenhum dos else if's ou else's restantes serão testados.

Sintaxe

A sintaxe de um if...else if...else declaração na linguagem de programação Objective-C é -

if(boolean_expression 1) {
   /* Executes when the boolean expression 1 is true */
} else if( boolean_expression 2) {
   /* Executes when the boolean expression 2 is true */
} else if( boolean_expression 3) {
   /* Executes when the boolean expression 3 is true */
} else {
   /* executes when the none of the above condition is true */
}

Exemplo

#import <Foundation/Foundation.h>
 
int main () {
   /* local variable definition */
   int a = 100;
 
   /* check the boolean condition */
   if( a == 10 ) {
      /* if condition is true then print the following */
      NSLog(@"Value of a is 10\n" );
   } else if( a == 20 ) {
      /* if else if condition is true */
      NSLog(@"Value of a is 20\n" );
   } else if( a == 30 ) {
      /* if else if condition is true  */
      NSLog(@"Value of a is 30\n" );
   } else {
      /* if none of the conditions is true */
      NSLog(@"None of the values is matching\n" );
   }
   
   NSLog(@"Exact value of a is: %d\n", a );
   return 0;
}

Quando o código acima é compilado e executado, ele produz o seguinte resultado -

2013-09-07 22:05:34.168 demo[8465] None of the values is matching
2013-09-07 22:05:34.168 demo[8465] Exact value of a is: 100
objetiva_c_decisão_making.htm