I have an integer, that can have values from 1 to 100.
I want to define the behavior of the game, depending on the value of the integer.
If it's smaller than 10 I want thing A to happen.
If it's equal to 10 I want thing B to happen.
If it's from 11 to 19 I want thing C to happen.
If it's equal to 20 I want thing D to happen.
... and so on.
Obviously, using this syntax:
function CheckIntegersValue (){
switch (IntValue){
case <10:
//do thing A goes here...
break;
case 10:
//do thing B goes here...
break;
case >10 && <20:
//do thing C goes here...
break;
... and so on,
is wrong. Is there a way to put several possible values (eg int between 1 & 9) in one case?
This was going to be a comment about the question regarding "switches are cheaper (or are they?)", but it got too long, sorry.
In JS, switch/case is a bit slower than if/else, and in C# it's a little faster (if/else is identical speed in both languages; I assume it gets compiled to the exact same bytecode). However, the difference is small enough that you have to loop millions of times for it to show up at all, never mind be noticeable. You should only use switch/case if it helps readability, not because of performance, since there is barely any difference.
In this case I would highly recommend doing if/else rather than Peter G's solution, which is clever but obscures what you're actually doing, and would be slower because the extra math and function call are, relatively speaking, significantly more expensive than the tiny speed difference between switch/case and if/else. When it comes to code, straightforward beats clever any day. You will thank yourself when you re-read your code later.
Afraid not. If it were just a few values, you can leave out the breaks in between like so:
switch (value) {
case 1:
case 2:
case 3:
lessthanequaltothree();
break;
case 4:
four();
case 5:
fourandfive()
break;
default:
everythingelse()
break;
}
but a switch is just a sophisticated if then else so you'll need
if ( value < 10 ) lessthanten;
else if ( value == 10 ) ten();
else if ( value < 20 ) tentotwenty(); // because the previous if's already denied <= 10
This isn't a great solution, but I figured I'd post it just in case no one had a better solution.
You could multiply by .1 (divide by 10) to get a float from 0 - 10.0. Then you can use Mathf.FloorToInt() to get an integer between 0 and 10. Then you will only have 11 possible cases.
function CheckIntegersValue (){
IntValue /= 10.0;//This will return a float
var finalInt : int = Mathf.FloorToInt(IntValue);
switch (finalInt){
case 0:
//do thing A goes here...
break;
case 1:
//do thing B goes here...
break;
case 2:
//do thing C goes here...
break;
//And so forth.
}
I converted a bunch of my code in UnityScript from IF’s to a switch. Theres about 12 cases, but the game runs noticeably slower, especially LateUpdate, which for some reason has stopped working entirely for some parts of the game that now need to use Update instead.
I’d say avoid using switches in UnityScript. Make enums if you want readability.
Hi,
To me this problem looks similar to printing grades of a student.
If marks(M) is 90 < M <= 100 the Grade is A+,
if 80 < M <= 90 the grade is A,
if 70 < M <= 80 the grade is B+ and so on…
I found this C program using switch statement to print grade of student
switch(marks/10){
case 10 :
case 9 :
/* Marks between 90-100 */
printf("Your Grade : A
" );
break;
case 8 :
case 7 :
/* Marks between 70-89 /
printf("Your Grade : B
" );
break;
case 6 :
/ Marks between 60-69 /
printf("Your Grade : C
" );
break;
case 5 :
case 4 :
/ Marks between 40-59 /
printf("Your Grade : D
" );
break;
default :
/ Marks less than 40 */
printf("You failed
" );
}