user765368
user765368

Reputation: 20346

Is there a cleaner way to structure these conditionals?

This is just a basic programming question regarding conditional if. Let say I have something like this:

if(a == 1 || a == 2 || a == 3)
{
   var $myvar = 10;
   // do stuff if the value of a is either 1, 2 or 3
}

if(a == 1)
{
   var total = $myvar + 1;
   // do stuff if the value of a is 1
}

if(a == 2 || a == 3)
{
   var total = $myvar + 1;
   // do stuff if the value of a is either 2 or 3
}

It's pretty clear what I'm trying to do in the code above. As you can see, we have some common conditions in here (for example, a == 1 is common in 2 of the 3 conditions). Also, please notice that I have a variable $myvar that I want to be declared ONLY if a == 1 || a == 2 || a == 3 and access it in my other two conditions. Can anybody think of another (possibly cleaner) way of doing this?

Upvotes: 0

Views: 138

Answers (6)

mcalex
mcalex

Reputation: 6778

do it backwards:

if (a < 1 || a > 3)
{ 
   return;
}
else
{
   var $myvar = 10;
   if (a == 1)
   {
      var total = $myvar + 1;
      // do a == 1 stuff 
   }
   else
   {
      total = $myvar + 1;
      // do a == 2 or a == 3 stuff
   }
}

Upvotes: 0

Dora Ball
Dora Ball

Reputation: 1

switch (a){
    case 1:
         do stuff;
         //DONOTBREAK HERE
    case 2:
    case 3:
         if(a== 2 || a == 3){
              //do stuff exclusive to 2 and 3
         }
         //do stuff universal to 1,2,3
         break;
    default:
       //failing case
}

Upvotes: 0

Samuel Cook
Samuel Cook

Reputation: 16828

var $myvar=0,total=0;
switch(a){
    case 1:
    case 2:
    case 3:
        $myvar = 10;
    case 1:
        total =$myvar + 1;
        break;
    case 2:
    case 3:
        total = $myvar + 1;
        break;
}

Upvotes: 0

Without knowing the specific requirements, There really isn't too terribly much for you to do to improve that.

depending on your requirements:

You can try the switch statement

switch(a)
{
    case 1:
    case 2:
    case 3:
        //logic for if a is 1, 2, or 3
    break;
    default:
        //if a is something else
    break;
}

and

if(a == 2 || a == 3)

can be changed to

else if(a == 2 || a == 3)

but that's mostly cosmetic, and has a minimal effect on runtime.

Upvotes: 2

a_m0d
a_m0d

Reputation: 12195

You could try something like the following:

if(a == 1 || a == 2 || a == 3)
{
   var $myvar = 10;
   // do stuff if the value of a is either 1, 2 or 3

   if(a == 1)
    {
       var total = $myvar + 1;
       // do stuff if the value of a is 1
    }

    if(a == 2 || a == 3)
    {
       var total = $myvar + 1;
       // do stuff if the value of a is either 2 or 3
    }
}

This way, the variable $myvar is accessible to everything inside the outer if-block. I'm not completely sure about the scoping rules of PHP, but in c++ (which the language was originally tagged with), $myvar will not be accessible outside the if block.

However, in Javascript, due to its scoping rules, $myvar will still be accessible outside the if-block.

Upvotes: 0

Hogan
Hogan

Reputation: 70523

if(a == 1 || a == 2 || a == 3)
{
   var $myvar = 10;
   // do stuff if the value of a is either 1, 2 or 3
  if(a == 1)
  {
     var total = $myvar + 1;
     // do stuff if the value of a is 1
  }
  else
  {
     var total = $myvar + 1;
     // do stuff if the value of a is either 2 or 3
  }

}

or better yet

if(a == 1 || a == 2 || a == 3)
{
   var $myvar = 10;
   // do stuff if the value of a is either 1, 2 or 3
   var total = $myvar + 1;

  if(a == 1)
  {
     // do stuff if the value of a is 1
  }
  else
  {
     // do stuff if the value of a is either 2 or 3
  }

}

The difference between the two is slight and you might use the first if it makes the code clearer -- also some languages will change the scope of a variable depending on where it is declared so that might impact which you use.

Upvotes: 4

Related Questions