Clear Filters
Clear Filters

While clause with multiple comparison strings

3 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 9 Feb 2024
Edited: Stephen23 on 9 Feb 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).

Sign in to comment.

Accepted Answer

Shivam
Shivam on 9 Feb 2024
Edited: Shivam on 9 Feb 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

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!