mardi 1 décembre 2015

Avoid Multiple nested if statement in C

I'm thinking to avoid the nested if statements occur in my code.

for (i = 0; i < N; i++) {
  if(x & y != 0) {
     if(z & k ==  z) {
        if(w & ( (c || u) && f)) { 
            //  Do something with the checked condition
        }
      }
    }
 } 
             `

First The for loop and then inside multiple if statements looks ugly . Also the values x, y , z, k etc are of form edgeBag[i].edgeWeight or even larger than that in width hence it looks more ugly.

One way i can think is to use variable to store the x, y, z etc and then use the variable inside the if statement to check the condition.

 for (i = 0; i < N; i++) {
 a = x;
 b = y; 
 ...
   if(a & b != 0) {
       ...
          ...
           // .....
    }
  }

In beginning i was not very annoyed by this but once this pattern started to occur very often it's annoying. Is there any way to avoid this pattern. Or a better way to state that. Thanks.

Aucun commentaire:

Enregistrer un commentaire