While clause with multiple comparison strings

1 visualización (últimos 30 días)
Andraz
Andraz el 9 de Feb. de 2024
Comentada: Andraz el 16 de Feb. de 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 comentario
Stephen23
Stephen23 el 9 de Feb. de 2024
Editada: Stephen23 el 9 de Feb. de 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).

Iniciar sesión para comentar.

Respuesta aceptada

Shivam
Shivam el 9 de Feb. de 2024
Editada: Shivam el 9 de Feb. de 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

Más respuestas (0)

Categorías

Más información sobre Manage Products en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by