Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

na_0022: Recommended patterns for Switch/Case statements

ID: Title

na_0022: Recommended patterns for Switch/Case statements

Priority

Mandatory

Scope

NA-MAAB

MATLAB Versions

All

Prerequisites

None

Description

Switch / Case statements must use constant values for the Case arguments. Input variables cannot be used in the Case arguments.

Correct:

function outVar = NA_0022_Pass(SwitchVar)
%# codegen
	switch SwitchVar
		case Case_1_Parameter % Parameter
			outVar = 0;
		case NA_0022.Case % Enumerated Data type
			outVar = 1;
		case 3 % Hard Code Value
			outVar = 2;
		otherwise
			outVar = 10;
	end
end
Incorrect:
function outVar = NA_0022_Fail(Case_1, Case_2, Case_3, SwitchVar)
%# codegen
	switch SwitchVar
		case Case_1
			outVar = 1;
		case Case_2
			outVar = 2;
		case Case_3 
			outVar = 3;
		otherwise
			outVar = 10;
		end
	end

Rationale

  • Verification and Validation

  • Code Generation

  • Simulation

See Also

  • NASA Orion style guideline jh_0026: Switch / Case statement

Last Changed

V3.0

Introduced in R2013a

Was this topic helpful?