Ternary: Inline if/else and switch/case
An inline conditional. Can function as an if/else of as a switch/case.
Usage:
If/else behavior:
tern(cond,a,b) returns: a if cond is true, else returns b
Switch/case behavior (>3 arguments):
tern(s,case1,value1,..,caseN,valueN,<defaultValue>), same as:
switch s
case case1
return value1
..
case caseN:
return valueN
otherwise:
return defaultValue (default = NaN)
Examples:
If/else behavior:
disp(tern(val>0,'value is positive!', 'value is negative'));
Switch/case behavior:
disp(tern(val,1,'value is one!', 2, 'value is two!', 'values is not 1 or 2'));
Cite As
David (2023). Ternary: Inline if/else and switch/case (https://www.mathworks.com/matlabcentral/fileexchange/81863-ternary-inline-if-else-and-switch-case), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0 |