Main Content

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

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

Model Configuration Pane: Code Generation / Code Style

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

Settings

on (default) | off

Default: on

On

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

Off

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

Examples

expand all

Compare the logic patterns in the generated code when you set Convert if-elseif-else patterns to switch-case statements to different values.

Here is generated code that generated using the default Convert if-elseif-else patterns to switch-case statements value off:

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

Here is the same code generated with Convert if-elseif-else patterns to switch-case statements set to on:

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

Recommended Settings

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

Programmatic Use

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

Version History

Introduced in R2009b