HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

If/else + Swith/case

08-20-2009, 01:57 AM#1
Joker
These are identical, no? I'm only used to if/else because of JASS, but switch/cases seem exactly the same. Do they have different uses?
08-20-2009, 02:48 AM#2
BlinkBoy
Quote:
Originally Posted by Joker
These are identical, no? I'm only used to if/else because of JASS, but switch/cases seem exactly the same. Do they have different uses?

they are pretty similar, however switch case is limited to a value like this:

Code:
int MyInt = 2;

switch ( MyInt ) {
case 1:
  cout<<"Your int is 1"<<endl;
  break;
case 2:
  cout<<"Your int is 2"<<endl;
  break;
default:
  cout<<"Muhaha your int isn't within the cases, hope you get a bug >:)"<<endl;
  break;
}

It's translated to:
Code:
int MyInt = 2;

if (MyInt == 1) {
    cout<<"Your int is 1"<<endl;
}
else if (MyInt == 2) {
    cout<<"Your int is 2"<<endl;
}
else {
  cout<<"Muhaha your int isn't within the if, hope you get a bug >:)"<<endl;
}
08-20-2009, 05:12 AM#3
Earth-Fury
Also note that switch/case constructs do not force cases in to blocks. If you want a new subscope, you have to add a block in:

Code:
switch(var) {
    case 1: {
        int i;
        break;
    }
    case 2: {
        int i;
        break;
    }
}

Switch/case constructs also allow fallthrough. when a "switch(var)" is reached, execution jumps to the first matching case, and continues until the end of the switch()'s block is encountered, or a break; statement is reached. this allows:
Code:
switch(var) {
  case 1:
  case 2:
  case 4:
    //do something for cases 1, 2 and 4
  case 5:
    //do something for cases 1, 2, 4 and 5
    break;
  case 6:
    //do something for only case 6
    break;
}
// do nothing for the value 3

please, mark intentional fallthrough (no break; statement after a block of code in a case) with a comment.

One final thing: Most languages really limit the values that can be checked with a switch. In C++, I do believe only integers work. In Java, integers and type safe enums both work, and I think some weird shit with classes also works.
08-20-2009, 05:28 AM#4
Veev
You can also use switch with char variables.
08-20-2009, 06:05 AM#5
Eleandor
Well, a char is an int in the end...

I think c allowed strings to be used too, but c++ removed that functionality.
08-20-2009, 10:41 PM#6
TheDamien
One important difference is that compilers will often generate jump tables for large switch statements.
08-22-2009, 05:09 PM#7
Joker
Quote:
Originally Posted by Earth-Fury
Also note that switch/case constructs do not force cases in to blocks. If you want a new subscope, you have to add a block in:
subscopes = ?
08-22-2009, 05:19 PM#8
Alevice
subscopes can have their own declarations that are unexistants anywhere esle. A subscope can see everything from its parent scope (and its subsequent parents), but a parent scope can't see whatever lies within subscopes.
08-22-2009, 07:01 PM#9
Vexorian
Quote:
Originally Posted by Joker
subscopes = ?
In C/c++/Java/ mediocre imitations A subscope is:

Code:
{
    int foo
    //stuff
}
// foo is gone!


In C++, they are rather cool because of constructors and destructors:

Code:
yadda yadday;
{
   someclass A() ;

   A.something();
}
//A 's deconstructor is called


yadda yadda;
Yes, it is important to use switches when doing stuff that looks like a switch, let the compiler do better code. Though probably some compilers are able to recognize your chained ifs and compile them as if they were switch...