Introduction
If it is required to compare an integer variable with a lot of different values, using of the if-statement may be very impractical. In this case the switch-statement may help. It has the following syntax:
switch(expression)
{
case label1:
list of statements
case label2:
list of statements
default:
list of statements
}
The expression must result in int or a compatible data type which can be converted implicitly. The default-label is optional. The label must result in a constant value. It is possible to use integer values like "0" or "12", constant variables, or double-quoted strings like "A". After the colon, one or more statements may follow. But blocks are also allowed. Moreover, each label has to be unique.
Labels are not an independent block of source code, but a marker where the code execution should be continued if the expression has the corresponding value. So after execution of the statements for label 1 the statements of label 2 will be executed and so on. In order to avoid that behavior, use the keyword break. If the expression does not match any of the given labels, the execution will be continued with the default-label.
The following sample code writes the name of the current day into the message window of the editor.
date d = GetDate();
switch(d.weekday)
{
case 0: WriteText("Sunday"); break;
case 1: WriteText("Monday"); break;
case 2: WriteText("Tuesday"); break;
case 3: WriteText("Wednesday"); break;
case 4: WriteText("Thursday"); break;
case 5: WriteText("Friday"); break;
case 6: WriteText("Saturday"); break;
}
Now, we are also able to shorten the usage of the if-statement from the last example for the MAS Script Effect in the following way:
color colCross = {0, 255, 255, 255};
int g_iCol;
void InitEffect()
{
g_iCol = 0;
SetBpm(300);
}
void RenderEffect()
{
int x, y;
switch(g_iCol)
{
case 0:
colCross.r = 255;
g_iCol = 1;
break;
case 1:
colCross.r = 155;
g_iCol = 2;
break;
default:
colCross.r = 0;
g_iCol = 0;
break;
}//switch[current color]
for(int x = 0; x < GetMatrixWidth(); x++)
{
for(int y = 0; y < GetMatrixHeight(); y++)
{
if(x == y)
SetPixel(colCross, x, y);
else if(GetMatrixWidth() - x-1 == y)
SetPixel(colCross, x, y);
else
SetPixel(BLACK, x, y);
}//for[each line]
}
}
Using Constant Variables
As mentioned earlier, it is possible to use variables declared as constants, but it is necessary that the compiler is able to compute the value during compilation time. Here is an example with a valid as well as an invalid case label.
const int label1 = 1;
const int label2 = GetMatrixWidth();
switch(<something>) {
case label1: do something
break;
case label2: do something else
break;
}
The first label (label1) is a valid case label since the compiler is able to compute the value of 1 during compilation time. The second label (label2) is invalid since it is computed during runtime and therefore it is not a constant value for the compiler, even though it is not possible to change its value later on.
The following examples are all valid case labels since the compiler can compute their values:
const int label1 = 2 + 4;
const int label2 = label1 + 3;
const int label3 = label2;
Using Double-Quoted Strings
It is also possible to use double-quoted strings for case labels. But they have to have the length of 1. Here is an example:
void writeText(string s)
{
for(int i = 0; i < s.length; i++)
{
switch(s[i])
{
case "A": do something; break;
case "B": do something with b; break;
...
}
}
}
|