While clause with multiple comparison strings

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2024-2-9
编辑:Stephen23 2024-2-9
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).

请先登录,再进行评论。

采纳的回答

Shivam
Shivam 2024-2-9
编辑:Shivam 2024-2-9
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Manage Products 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by