switch Statement

Transfer control to a different statement block within the switch body depending on the value of a selector.

[Attribute] switch( Selector ) { case 0 : { StatementBlock; } break; case 1 : { StatementBlock; } break; case n : { StatementBlock; } break; default : { StatementBlock; } break;

Parameters

Attribute

An optional parameter that controls how the statement is compiled. When no attribute is specified, the compiler may use a hardware switch or emit a series of if statements.

Attribute Description
flatten Compile the statement as a series of if statements, each with the flatten attribute.
branch Compile the statement as a series of if statements each with the branch attribute. Note: When you use Shader Model 2.x or Shader Model 3.0, each time you use dynamic branching you consume resources. So, if you use dynamic branching excessively when you target these profiles, you can receive compilation errors.
forcecase Force a switch statement in the hardware. Note: Requires feature level 10_0 or later hardware.
call The bodies of the individual cases in the switch will be moved into hardware subroutines and the switch will be a series of subroutine calls. Note: Requires feature level 10_0 or later hardware.

Selector

A variable. The case statements inside the curly brackets will each check this variable to see if the SwitchValue matches their particular CaseValue.

StatementBlock

One or more statements.

Remarks

[branch] switch(a)
{
    case 0:
        return 0; 
    case 1:
        return 1; 
    case 2:
        return 3; 
    default:
        return 6; 
}

Is equivalent to:

[branch] if( a == 2 )
    return 3;
else if( a == 1 )
    return 1;
else if( a == 0 )
    return 0;
else
    return 6;

Here are example usages of forcecase and call flow control attributes:

[forcecase] switch(a)
{
    case 0:
        return 0; 
    case 1:
        return 1; 
    case 2:
        return 3; 
    default:
        return 6; 
}

[call] switch(a)
{
    case 0:
        return 0; 
    case 1:
        return 1; 
    case 2:
        return 3; 
    default:
        return 6; 
}

Requirements

Requirement Value
Header
Urlmon.h

See also

Flow Control