Switch Case or if-else?
Mostra commenti meno recenti
Hello,
I'm writing a script which calculates a value of "R" which is then compared to several inequality limits. The limits are:
R <= 1.2
1.2 < R <= 1.45
1.45 < R <= 1.66
1.66 < R <= 1.93
R > 1.93
Once R satisfies the conditional statement a subroutine is prompted and 1 of 5 subsequent scripts is run.
Is switch case or if-else better for this?
Risposta accettata
Più risposte (2)
Dyuman Joshi
il 21 Mag 2021
2 voti
if-else would be better in this case because you have a range of values of R (a variable).
switch is useful when you have descrete values of a variable.
Stephen23
il 18 Lug 2023
"Switch statement cannot judge the range of R. It may be possible but it must be very tricky."
switch true
case R <= 1.2
disp('condition 1');
case R <= 1.45
disp('condition 2');
case R <= 1.66
disp('condition 3');
case R <= 1.93
disp('condition 4');
otherwise % R > 1.93
disp('condition 5');
end
Categorie
Scopri di più su Variables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!