Convert if-elseif-else patterns to switch-case statements

Description

Specify whether to generate code for if-elseif-else decision logic as switch-case statements.

This readability optimization works on a per-model basis and applies only to:

  • Flow charts in Stateflow® charts

  • MATLAB® functions in Stateflow charts

  • MATLAB Function blocks in that model

Category: Code Generation > Code Style

Settings

Default: on

On

Generate code for if-elseif-else decision logic as switch-case statements.

For example, assume that you have the following logic pattern:

if (x == 1) {
       y = 1;
} else if (x == 2) {
       y = 2;
} else if (x == 3) {
       y = 3;
} else {
       y = 4;
}

Selecting this check box converts the if-elseif-else pattern to the following switch-case statements:

switch (x) {
     case 1:
       y = 1; break;
     case 2:
       y = 2; break;
     case 3:
       y = 3; break;
     default:
       y = 4; break;
}
Off

Preserve if-elseif-else decision logic in generated code.

Command-Line Information

Parameter: ConvertIfToSwitch
Type: character vector
Value: 'on' | 'off'
Default: 'on'

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityOff
EfficiencyOn (execution, ROM), No impact (RAM)
Safety precautionNo impact

Related Topics