Your code looks fine. However, you can simplify the structure by adding target = 0; before your loop and omitting the else statement.
You might also consider using find(X==M,1), replacing X with each of your variables and then using min to find the index of the first occurrence of M, equivalent to your iterator i when you hit the break statement.
In any case, your use of if...break...else is not incorrect.
2 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/250213-can-i-use-if-break-else-inside-the-same-loop#comment_317902
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/250213-can-i-use-if-break-else-inside-the-same-loop#comment_317902
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/250213-can-i-use-if-break-else-inside-the-same-loop#comment_317939
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/250213-can-i-use-if-break-else-inside-the-same-loop#comment_317939
Sign in to comment.