Skip to content
Related Articles

Related Articles

Avoiding elif and ELSE IF Ladder and Stairs Problem

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Jun, 2022

This article focuses on discussing the elif and else if ladder and stairs problem and the solution for the same in the C and Python Programming languages.

The ELIF and ELSE IF Ladder and Stairs Problem

There are programmers who shy away from long sequences of IF, ELSE IF, ELSE IF, ELSE IF , etc. typically ending with a final ELSE clause. In languages with ELSE IF or  elif syntax, the pattern is called an “ELSE IF ladder.” In languages without ELSE IF or elif , each conceptual ELSE IF is written as an IF placed within the  ELSE  part of its predecessor. With indenting, it looks like descending an upside-down staircase.

  • This forms an undesirable, hard-to-read, worse-to-enhance, error-prone programming structure. 
  • With indenting, the staircase code falls off the right side of the screen. 
  • For some programmers, the problem is esthetics, while others worry about maintainability. 
  • Sometimes, the syntax genuinely gets in the way of expressing the logic that needs to express. 

Fundamentally this pattern expresses both prioritization and a short-circuiting: 

  • The prioritization is that statements are processed only for the first TRUE among a set of conditions. 
  • The short-circuiting is that after the first condition is found to be true, no further test condition expressions are even evaluated. This matters because condition expressions may invoke some functions, such as IF (42 < somefunction( x ) ). . . , where some functions might be stateful and have side-effects. 

Any alternative must maintain these semantics. Furthermore, sometimes there are statements that need to be executed only if some prior conditions fail, but need to execute for all the later conditions. 

Example: 

C




// Many people find this code 
// undesirable to work with.
if (a < b) 
{
    // a<b statements
}
else if (b < c) 
{
    // b<c statements
}
else if (c < d) 
{
    // Statements before every 
    // remaining possibility
    a = 45;
    
    // c<d statements
}
else if (d < e) 
{
    // Statements before every 
    // remaining possibility
    a = 45;
    
    // d<e statements
}
else if (e < f) 
{
    // Statements before every 
    // remaining possibility
    a = 45;
    
    // e<f statements
}
else 
{
    // Statements before every 
    // remaining possibility
    a = 45;
    
    // else statements
}


Python3




# Many people find this code 
# undesirable to work with.
  
if (a < b):
    # a<b statements
      
elif (b < c):
    # b<c statements
      
elif (c < d):
    # statements before every 
    # remaining possibility
    a = 45
      
    # c<d statements
      
elif (d < e):
    # statements before every 
    # remaining possibility
    a = 45
      
    # d<e statements
      
elif (e < f):
    # statements before every 
    # remaining possibility
    a = 45
      
    # e<f statements
      
else:
    # statements before every 
    # remaining possibility
    a = 45
      
    # else statements
      
# end of IF-ELIF-ELSE   
pass  
  
# Contributed by David A. Kra


Note that this repetition of statements that apply to each of the later possibilities ( a = 45 ) violates the “Don’t Repeat Yourself” (DRY) principle. Any change must be repeated after each condition.

Two-step Approach To Eliminate ELSE IF and ELSE 

Instead of the ELSE IF and ELSE code above, the following approach eliminates the need for them, while keeping the prioritization and short-circuiting semantics:

  1. Place a set of independent IF statements within a single-iteration loop.  Do not use any ELSE IF or ELSE syntax. 
  2. Add a loop exiting statement as the last of the statements within each  IF.  BREAK is preferred to  CONTINUE. The python code offers a for and a while version of the loop. BREAK must be used with the while version of the loop.

Example:

C




// This is the ELSEless equivalent.
do 
  // This is a one pass WHILE loop.
    if (a < b) 
    {
        // a<b statements
        break;
    }
    if (b < c) 
    {
        // b<c statements
        break;
    }
  
    // Statements for every remaining 
      // possibility
    a = 45;
  
    // More conditions
    if (c < d) 
    {
        // c<d statements
        break;
    }
    if (d < e) 
    {
        // d<e statements
        break;
    }
    if (e < f) 
    {
        // e<f statements
        break;
    }
  
    // else statements
    q = 75;
  
    // break unnecessary. 
    // Already at end of loop.
} while (0); 
  
// Once was enough. Do not repeat it.


Python3




# This is the ELSEless equivalent.
  
# for version # !!!! Use any one and 
# only one value in the iteration set. !!!!
# forversion="""for i in ("once - elseless"):"""
  
# while version # !!!! requires a BREAK 
# at the bottom !!!!
# whileversion="""while(True):"""
while (True):
    
     if (a < b):
         # a<b statements
         break  
            
     if (b < c):
         # b<c statements
         break   
  
     # statements for every remaining 
     # possibility indenting aligns
     # with the if's above
     a = 45
      
     if (c < d):
          # c<d statements
          break   
            
     if (d < e):
          # d<e statements
          break   
            
     if (e < f):
          # e<f statements
          break   
       
     # else statements
     # indenting aligns with the if's above
     q = 75
        
     # for version: break unnecessary. 
     # Already at end of one-pass loop.
     # while version: break absolutely 
     # required for the while version
     break # required for the while version
      
pass  # past end of else-less IF-ELIF-ELSE
  
# Contributed by David A. Kra


Explanation: We rarely, if ever, intentionally code a loop that will always be used for exactly one iteration, but here we do that. Most programming languages provide a loop control statement, such as BREAK to exit a loop completely, or CONTINUE, to jump to the loop’s next iteration. However, in this one-pass loop usage, there will not be and must not be any next iteration. 

This solution exploits these iteration statements in place of ELSE IF and ELSE. The BREAK, or CONTINUE statement causes all further statements inside the loop to be bypassed. When the loop is specified so as to have only one iteration, even a CONTINUE statement leads to exiting the loop. 

In the code sample, either CONTINUE or BREAK will work with the C version or the for loop version in python. The while loop python version requires the use of the break statement, including one after the final else-equivalent statements. 

Also, in between the IF statements, there can be additional statements that execute before testing any of the later conditions.  

Processing Cost or Overhead: The python version does experience the minimal overhead of setting up the loop. The overhead in C is even less, and if using a clever optimizing compiler, there might be no overhead at all.

Summary

At first glance, this idiom looks like an unnecessary trick. Upon more careful consideration, it makes the application easier to understand and maintain. I would use it with compiled languages, especially with an optimizing compiler. I would also use it in interpreted languages, except in a performance-critical hotspot. Even there, I would use it initially and revert to if, elif, elif, elif … else only after full application debugging.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!