Another one! Switch this time!
Guys,
Thanks for all the help so far.
Another question.
With switch I was under the impression that I could test multiple case conditions at once.
For example I am doing a swich on the month of the year and tried doing this in the case statement:
case 1,3,5,7,8,10,12:
but it hasn''t worked. Did I pick this up from VB?? Anyway, is there a way of doing this without repeating the code loads of times and without using a more messy ''if'' statement?
what you want is:
switch (ch) {
case 'a':
case 'b':
case 'c':
do stuff
break;
default:
break;
}
you have to use a seperate case line for each of the elements you are switching on
[edited by - trainedmonkey on July 9, 2002 12:07:18 PM]
switch (ch) {
case 'a':
case 'b':
case 'c':
do stuff
break;
default:
break;
}
you have to use a seperate case line for each of the elements you are switching on
[edited by - trainedmonkey on July 9, 2002 12:07:18 PM]
The best way to do it is using something called ''fallthrough:''
switch(var)
{
case CASE_1:
case CASE_ONE:
case CASE_UNO:
{
...
break;
}
case CASE_2:
case CASE_TWO:
case CASE_DUO:
{
...
break;
}
}
If you don''t include the ''break'' command, the code automatically goes into the next case, even though the switch doesn''t match it. In my example, if var is CASE_1, the program will go to ''case CASE_1:'', then flow through to ''case CASE_ONE:'', then ''case CASE_UNO:''. It runs the block of code under CASE_UNO until it gets to the ''break'' command, where it leaves the switch statement.
Fallthrough is particularly useful if you want to have two cases where one does something extra. Example:
switch(var){
case CASE_EXTRA:
{
CallExtraFunction();
}
case CASE_NORMAL:
{
CallNormalFunction();
}
}
If var is CASE_EXTRA, both functions get called; if var is CASE_NORMAL, only the second one gets called.
Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net
switch(var)
{
case CASE_1:
case CASE_ONE:
case CASE_UNO:
{
...
break;
}
case CASE_2:
case CASE_TWO:
case CASE_DUO:
{
...
break;
}
}
If you don''t include the ''break'' command, the code automatically goes into the next case, even though the switch doesn''t match it. In my example, if var is CASE_1, the program will go to ''case CASE_1:'', then flow through to ''case CASE_ONE:'', then ''case CASE_UNO:''. It runs the block of code under CASE_UNO until it gets to the ''break'' command, where it leaves the switch statement.
Fallthrough is particularly useful if you want to have two cases where one does something extra. Example:
switch(var){
case CASE_EXTRA:
{
CallExtraFunction();
}
case CASE_NORMAL:
{
CallNormalFunction();
}
}
If var is CASE_EXTRA, both functions get called; if var is CASE_NORMAL, only the second one gets called.
Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement