If-statements won't break when conditions aren't met

4 views (last 30 days)
Hi there!
I am working on a mainscript in Matlab and I have troubles with determining why my code doesn't break when it does not meet the conditions that I have placed within the if statement. Anyone who has got an idea of this?
I hope there are someone out there who has an answer for this!
Best regards
Kristian
  2 Comments
James Tursa
James Tursa on 5 May 2015
@Kristian: It is an abuse of this site and a disservice to the readers and contributors for you to edit out the content of your question after you have received answers.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 4 May 2015
You are using "if" with a vector of values. "if" is considered true if all of the values in the vector are true. The condition
if vector_of_values
is equivalent to
if all(vector_of_values)
What you probably want in your situation is to "break" if any of the values are true. To achieve that, recode as
if any(vector_of_values)
For example change
if beamLength < loadPositions | loadPositions <= 0 | ~isnan(loadPositions);
to
if any(beamLength < loadPositions | loadPositions <= 0 | ~isnan(loadPositions))
You should also be considering using any() on the individual parts of the construction, and also considering short-circuiting the test:
if any(beamLength < loadPositions) || any(loadPositions <= 0) || any(~isnan(loadPositions))
However, your code appears to be attempting to validate that the values are all valid, and to "break" if they are all valid. In such a case, you do not want to or the portions together, you want to and them together, and you would want to use all() either explicitly or implicitly. I do not recommend using all() implicitly as it then is not clear whether you specifically want the all() behaviour or if you did not know about it. I suspect what you want for your code is
if all(beamLength < loadPositions & loadPositions <= 0 & ~isnan(loadPositions))
You could also write that with explicit short-circuiting using
if all(beamLength < loadPositions) && all(loadPositions <= 0) && all(~isnan(loadPositions))
Is it correct that the user is to input strictly negative values for beamLength and non-positive values for loadPositions? It is possible and perhaps meaningful for your purpose, but it would be much less common than expecting the values to be >= 0 instead of <= 0
  2 Comments
Walter Roberson
Walter Roberson on 4 May 2015
I might be misunderstanding your code. My reading of your code, with all the repeated input() and so on, was that your code is attempting to detect bad input and to re-prompt and re-input until you received valid input, and only then do the calculation. If I am correct then when you receive bad input you do not want to break(), as continuing on is the method that you use to prompt and fetch the repaired values.
The multiple "while true" are confusing me. You need to clean up the indentation on the code so I can figure out which parts are nested and which are not.
The usual structure for code that validates input is to loop,
set GotValidInput false
while not GotValidInput
prompt
get some input
if the input shows the user asked to cancel, break the loop
validate the input
if validation SUCCEEDS then set GotValidInput to True and break out of the loop
you had bad input if you got here. Complain
end of loop
if GotValidInput is not set at this point then the user asked to cancel, so do something appropriate and get out of here
You only got here if input was valid. Run the calculation. Or start a new loop concerned with fetching and validating another input value.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 4 May 2015
Edited: James Tursa on 4 May 2015
This line does no do what you think it does:
if beamLength < positions(:) | positions(:) < 0 | ~isnan(positions)
If positions is a vector, then beamLength < positions(:) will be a vector result, not a scalar result. So using it as the argument to the if test isn't appropriate. Maybe you meant this instead?
if any(beamLength < positions(:)) || any(positions(:) < 0) || any(isnan(positions(:)))
Similar comments apply to your other if tests as well.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!