Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/15/03 12:17
Read: times


 
#43440 - RE: Switch Case and If Else If
Responding to: ???'s previous message
I have a policy of suggesting that people never use the "else if" construct. Taken to an extreme you get
code that is next to impossible to read.

If the issue is one of deciding on which functions or pieces of code to process based upon conditions, try to use Switch with Cases wherever possible. Even with a sparse population of the case values that directs the compiler away from a table driven branch function the compiler generated branch tree is probably more efficient than the branch tree generated from a seres of nested if-else logic.

If the conditions are more complicated than what a switch can handle then I recommend writing the C code with fully nested IF structures .....

if(cond1)
{
   if(cond2)
   {
     ...
   }
   else
   {
     ...
   }
}
else
{
   if(cond3)
   {
     ...
   }
   else
   {
      if(cond4)
      {
         ...
      }
   }
}


This approach is much easier to read, maintain, and update. It is also recommended by most corporate coding standards I have read. "else if" should be removed from C as far as I am concerned.

Michael Karas



List of 8 messages in thread
TopicAuthorDate
Switch Case and If Else If            01/01/70 00:00      
   RE: Switch Case and If Else If            01/01/70 00:00      
      RE: Switch Case and If Else If            01/01/70 00:00      
         RE: Switch Case and If Else If            01/01/70 00:00      
   RE: Switch Case and If Else If            01/01/70 00:00      
      RE: Switch Case and If Else If            01/01/70 00:00      
         RE: Switch Case, abhishek            01/01/70 00:00      
            RE: Switch Case, abhishek            01/01/70 00:00      

Back to Subject List