Quote:
|
Originally Posted by Anthony Kesich
On that note, does anybody know if you can nest the ?: operator? i.e. x=(condtion 1) ? 1 : (condition 2) ? 2 : (condition 3) ? 3 : ... (condition n) ? n : 0
|
yes, you can nest the ?: operator, although i am not certain of the order of operations so you might want to type it like
x=(condition1 ? 1 : (condition2 ? 2 : (condition3 ? 3 : ...(condition n ? n : 0)...))
also a range of case statements cannot be used as in PBasic however the following is a way to simulate this.
switch(statement) {case 1:
case 2:
case 3:
doSomething();
break;
case 4:
case 5:
case 6:
doSomethingElse();
break;
}
However this would not be practical for a very large range of values.
Also, if the compiler compiles the sane way as the C compiler I have studied, a switch/case statement uses up more code space than if/else statements, especially when there is a large range of skipped values such as. ..
switch(statement) {case 1:
doSomething();
break;
case 1000000:
doSomethingElse();
break;
}
Switch/case statements are quicker than several if/else statements though because it jumps to a calculated address rather than having to make many comparisons, but this will not be noticeable if there are only a few choices.