else if( ... ) ... (Idiom)

A technique for stacking if conditions in a script.

Although there is no 'else if' keyword, because the if follows the else and is separated by a space, the if block is treated as if it were a single statement. Here we omit the braces surrounding the statement executed in the else condition of a leading if block. We then place another in its else statement and so on. Like this:

   if(aCondition)

   {

      some code

   }

   else if(aCondition)

   {

      some code

   }

   else if(aCondition)

   {

      some code

   }

   else

   {

      some code

   }

By putting in the braces, it becomes clearer. Actually, what we have really done is this:

   if(aCondition)

   {

      some code

   }

   else

   {

      if(aCondition)

      {

         some code

      }

      else

      {

         if(aCondition)

         {

            some code

         }

         else

         {

            some code

         }

      }

   }

It is really just a plain old if() else block nested several times. By leaving off the braces (against our recommendations otherwise), in this circumstance it actually makes the code clearer.

See also:Code block delimiter {}, else ..., if( ... ) ..., if( ... ) ... else ...