wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Warcraft III Modding > Developer's Corner > Warcraft Editing Tools > Programming
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 08-20-2009, 01:57 AM   #1
Joker
User
 
Joker's Avatar
 
Join Date: Sep 2006
Posts: 687

Joker will become famous soon enough (32)Joker will become famous soon enough (32)

Default If/else + Swith/case

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?
Joker is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 08-20-2009, 02:48 AM   #2
BlinkBoy
User
 
BlinkBoy's Avatar


Respected User
 
Join Date: Dec 2003
Posts: 835

Submissions (4)

BlinkBoy has a spectacular aura about (97)BlinkBoy has a spectacular aura about (97)BlinkBoy has a spectacular aura about (97)BlinkBoy has a spectacular aura about (97)

Outstanding Tutorial

Default

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;
}
__________________
Tools:
NeoDex - a Gmax and 3ds Max modeling Toolset for Wc3!

Learn to animate! check out my: Basic Animation Tutorial!

Currently working at a sequel to my animation tutorial.
BlinkBoy is offline   Reply With Quote
Old 08-20-2009, 05:12 AM   #3
Earth-Fury
Two Blue
 
Earth-Fury's Avatar
 
Join Date: Mar 2003
Posts: 2,179

Submissions (7)

Earth-Fury is just really nice (343)Earth-Fury is just really nice (343)Earth-Fury is just really nice (343)Earth-Fury is just really nice (343)

Send a message via MSN to Earth-Fury
Default

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.
__________________
Useful Links:
My Resources:
Good Resources:
Search
Tutorials
Ability Guide

UnitMaxState
BonusMod

AutoIndex
ARGB
TimerUtils

Earth-Fury is offline   Reply With Quote
Old 08-20-2009, 05:28 AM   #4
Veev
User
 
Join Date: Nov 2006
Posts: 199

Veev will become famous soon enough (63)Veev will become famous soon enough (63)Veev will become famous soon enough (63)

Default

You can also use switch with char variables.

Last edited by Veev : 08-20-2009 at 05:29 AM.
Veev is offline   Reply With Quote
Old 08-20-2009, 06:05 AM   #5
Eleandor
User
 
Join Date: Jan 2006
Posts: 118

Eleandor is on a distinguished road (12)

Default

Well, a char is an int in the end...

I think c allowed strings to be used too, but c++ removed that functionality.
Eleandor is offline   Reply With Quote
Old 08-20-2009, 10:41 PM   #6
TheDamien
User
 
TheDamien's Avatar
 
Join Date: Jun 2007
Posts: 136

TheDamien is on a distinguished road (20)

Default

One important difference is that compilers will often generate jump tables for large switch statements.
TheDamien is offline   Reply With Quote
Old 08-22-2009, 05:09 PM   #7
Joker
User
 
Joker's Avatar
 
Join Date: Sep 2006
Posts: 687

Joker will become famous soon enough (32)Joker will become famous soon enough (32)

Default

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 = ?

Last edited by Joker : 08-22-2009 at 05:33 PM.
Joker is offline   Reply With Quote
Old 08-22-2009, 05:19 PM   #8
Alevice
It feels good
 
Alevice's Avatar
 
Join Date: Mar 2006
Posts: 1,305

Alevice is a jewel in the rough (190)Alevice is a jewel in the rough (190)Alevice is a jewel in the rough (190)

Default

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.
__________________
_-|-_

Alevice.jones is SC2 Beta, losers.

Check out my never updated deviantart gallery!!

Kalimdor Raiders (Race Contest #1) Pastebin Page
Cavern Crawlers (Race Contest #2) Pastebin Page
Airship Race Motherfuckers!
Quote:
Originally Posted by http://ajaxian.com/archives/would-you-like-a-_-with-that-new-library-gives-js-what-it-should-have#comment-276203
- Dont solve problems that dont exist.
- Improve the wheel, dont reinvent it.
- Port the wheel if it doesnt exist in your environment.
- Integrate the wheel into your project.
- Make sure you can replace your wooden wheel for a rubber one if someone else invents it.
Alevice is offline   Reply With Quote
Old 08-22-2009, 07:01 PM   #9
Vexorian
Free Software Terrorist
 
Vexorian's Avatar


Technical Director
 
Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)

Hero Contest #3 - 2nd Place

Default

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...
__________________
Zoom (requires log in)Wc3 map optimizer 5.0
Someone should fix .wav sound in this thing.
Zoom (requires log in)JassHelper 0.A.2.A
Turns your simple code into something that is complicated enough to work.
Faster != more useful
Vexorian is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 10:19 PM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services