While clause with multiple comparison strings

1 vue (au cours des 30 derniers jours)
Andraz
Andraz le 9 Fév 2024
Commenté : Andraz le 16 Fév 2024
I'm still a newbie in matlab coding therefore I need a bit help. It seems there is a problem in the following code. Whatever string I enter (including ScenarioA, ScenarioB, ScenarioC or ScenarioD) I always get the matching condition. For info - I have no problem with just one comparison string. Thanks in advance for any help.
Andrazko
%%%%%%%%%%%%%%%%
Matlab code:
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
while SelectedScenario ~= "ScenarioA" || SelectedScenario ~= "ScenarioB" || SelectedScenario ~= "ScenarioC" || SelectedScenario ~= "ScenarioD"
fprintf("Your selected scenario isn't listed! ");
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
end
%%%%%%%%%%%%%%%%
Command Window printout:
Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"!
ScenarioA
Your selected scenario isn't listed! Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"!
%%%%%%%%%%%%%%%%
  1 commentaire
Stephen23
Stephen23 le 9 Fév 2024
Modifié(e) : Stephen23 le 9 Fév 2024
Your logic is incorrect. The mistake you are making is corrected by understanding De Morgan's law:
In any case, do not chain together lots of individual logical comparisons: just use ISMEMBER or MATCHES on the whole list of possible options, with one single negation out in front. In other words, learn to think in terms of arrays (not lots of individual commands).

Connectez-vous pour commenter.

Réponse acceptée

Shivam
Shivam le 9 Fév 2024
Modifié(e) : Shivam le 9 Fév 2024
Hi Andraz,
Upon reviewing the code snippet you have shared, I notice there's a logical misstep causing the persistent loop condition regardless of the input string. Your loop intends to prompt the user until one of the specified scenarios is entered ('ScenarioA,' 'ScenarioB,' 'ScenarioC,' or 'ScenarioD').
The issue is because of the logical operator OR (||) operator used because the while condition will always evaluate to true because a string cannot be simultaneously different from all the other strings.
To resolve this, you should use the AND (&) logical operator. This operator will ensure that the loop only continues if the entered SelectedScenario does not correspond with any of the four provided scenarios.
You can refer to the following workaround:
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
while SelectedScenario ~= "ScenarioA" && SelectedScenario ~= "ScenarioB" && SelectedScenario ~= "ScenarioC" && SelectedScenario ~= "ScenarioD"
fprintf("Your selected scenario isn't listed! ");
SelectedScenario = input('Select the scenario. Choose between "ScenarioA", "ScenarioB", "ScenarioC" and "ScenarioD"! \n',"s");
end
I believe the explanation and modified code address the issue.
Thanks
  1 commentaire
Andraz
Andraz le 16 Fév 2024
Hi Shivam,
Now it works. Thanks a lot!
Andraz

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Manage Products dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by