Coding-Trick: Switch-Case as unWorkable for loop

While working, I thought I could provide few tips on coding in my blog.

So, I thought to share a "short code" in a situation where you want to initialize some values. Each value is initialized by different function. You know the functions but you don't know how many values to initialize.  Uninitialized values should be 0.

Suppose values to initialize

a = f1(); 
b = f2(); 
c = f3(); 
d = f4(); 

 A normal Switch case for the above Situation :

Switch(number_of_values_to_initialize) {

case 1:
          a = f1();
          b = 0;
          c = 0;
          d = 0;
          break;

case 2:
          a = f1();
          b = f2();
          c = 0;
          d = 0;
          break;

case 3:
          a = f1();
          b = f2();
          c = f3();
          d = 0;
          break;

case 4:
          a = f1();
          b = f2();
          c = f3();
          d = f4();
          break; }


 The Shortcut for This Code is :


switch(activated_l){
             case 4:
                 d=f4();
             case 3:
                  c=f3();
             case 2:
                 b=f2();
             case 1: 
                 a=f1();
        }
       
        switch(activated_l){
             case 1:
                b=0;  

           case 2:
                c=0

           case 3:
                d=0

       }


 Can you explain How? ;)

No comments:

Post a Comment

YOU MAY ALSO LIKE:

  • Eagle Guyz Far, far away, in another parallel universe, where you…
  • The Sparks of Happiness do Freeze Away As per Psychologist : ,…
  • Poem: The travellers of love "When you are certain you want to spend the rest of your…