0

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?

user765368
  • 19,590
  • 27
  • 96
  • 167
  • 6
    you tagged php, java, javascript, c++, please take time to ask a question, and make a real title – Ibu Nov 14 '12 at 18:15
  • Use a switch statement on a. – Blaise Swanwick Nov 14 '12 at 18:16
  • @Michael if this is [tag:language-agnostic], it's unanswerable. Scope does not work the same way on all languages. I'm guessing it's JavaScript. – bfavaretto Nov 14 '12 at 18:17
  • I would probably do a Var total = $myvar + 11;. Your code seem to work fine (depending on the context it is used in). If you want opinions then maybe use http://codereview.stackexchange.com instead. – Ammar Nov 14 '12 at 18:18
  • @bfavaretto, OP tagged it with many languages; I took that to mean he wanted a solution that's not tied to any particular one. If you can find a `c-style-language` tag, that'd work. – Michael Petrotta Nov 14 '12 at 18:19
  • @bfavaretto - It is answerable as long as scoping issues are included in the answer. – Hogan Nov 14 '12 at 18:22
  • 1
    user765368, do you favor any particular one of the four languages you tagged this question with? – Michael Petrotta Nov 14 '12 at 18:22
  • Using numeric values for control makes them "magic numbers" which should be avoided: http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad – Devon_C_Miller Nov 14 '12 at 18:39

6 Answers6

4
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.

Hogan
  • 69,564
  • 10
  • 76
  • 117
2

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.

0

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.

a_m0d
  • 12,034
  • 15
  • 57
  • 79
0
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;
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
0
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
}
Dora Ball
  • 1
  • 1
  • um... have you looked at [This answer?](http://stackoverflow.com/questions/13384737/is-there-a-cleaner-way-to-structure-these-conditionals#answer-13384821) – Ibu Nov 14 '12 at 18:22
  • This is a C only answer -- the question was language agnostic. – Hogan Nov 14 '12 at 18:23
  • Do not most languages fall through cases if unbroken? I'm not intimately familiar with the vast majority, but my solution works in any language that lets a switch structure fall through. (syntax modification obviously may be needed) – Dora Ball Nov 14 '12 at 18:26
  • @DoraBall - nope most languages do not do this. C and C++ do -- C# does not, java took it out -- it is a HORRIBLE "feature" that has cost millions of $$$ because leaving out a simple element (break) does not get caught as an error but instead causes a bug. In C# you can add a goto statement to get the same effect. http://stackoverflow.com/questions/174155/switch-statement-fallthrough-in-c This is why I said your answer is C only; because it is. – Hogan Nov 14 '12 at 18:58
0

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
   }
}
mcalex
  • 6,628
  • 5
  • 50
  • 80