Use of break in if?
307 views (last 30 days)
Show older comments
Here is my code i Want to use break/continue after First if Ends. as i have mention it there. but we cant use break in IF. I need alternative of it. Note: there are two times if(Stroke_counter==1) in code. that is not because of mistake
if(Stroke_counter==1)
if (((S==1)||(E==1)) &&( (y==2)) )
msgbox('This is One','Recognize');
end
elseif( (x==1)&&(y==1)&&(x==1) &&(y==1) &&(S==2))
msgbox('This is two','Recognize');
end
elseif((x==1)&&(y==1)&&(x~=1))
msgbox('This is three','Recognize');
end
beak;
else
msgbox('Not Found...','error');
end
else
msgbox('Not in 1 step','error');
end
if(Stroke_counter==1)
if (((S==1)||(D==1)) &&( (y==2)) )
msgbox('This is four','Recognize');
end
elseif( (x==1)&&(y==1)&&(D==1) &&(y==1) &&(S==2))
msgbox('This is five','Recognize');
end
elseif((x==1)&&(F==1)&&(x~=1))
msgbox('This is Six','Recognize');
end
else
msgbox('Not Found...','error');
end
else
msgbox('Not in 2 step','error');
2 Comments
David Sanchez
on 20 Aug 2014
What is the point on using a break within an if statement?
If the condition is fulfilled, the following else will not count in your code anyway.
Lorenzo TORRICELLI
on 15 Apr 2021
For example, if a block of instructions is common to a number of conditions, whereas a certain case is exceptional.
Accepted Answer
Adam
on 20 Aug 2014
If you want to break after the first If ends then you won't be in the If to place your break/continue. And if you were able to place one there then why not just delete the second if since it would never execute.
3 Comments
Adam
on 20 Aug 2014
Just use a boolean then as
answerFound = false;
then update it in each if statement and then AND it together with the following if statements as e.g.
if( ~answerFound && Stroke_counter==1)
To be honest though this sounds like such a large block of code it should be a function by itself (probably many, but that's a side issue) so you can just put
return
to return early from the current function and then carry on your algorithm in the function that calls this one.
More Answers (1)
Jan
on 15 Apr 2021
"MAtlab donot work in more than 6 (if() elseif() else end)" - of course Matlab handles much more nested IF branchs than 6.
Checking a lot of different conditions with a pile of IF commands creates code, which is hard to read and to debug. It is too prone to typos like in your code:
((x==1) && (y==1) && (x~=1))
This cannot be TRUE, because x==1 && x~=1 cannot happen. So it is not Matlab, but the programming style, which causes problems here.
This is not useful also:
elseif( (x==1) && (y==1) && (x==1) && (y==1) &&(S==2))
% ^^^^^^^^^^^^^^^^ tested twice ?!
Your code is not valid at all due to the orphand end commands:
if (((S==1)||(E==1)) &&( (y==2)) )
msgbox('This is One','Recognize');
end % <== Nope, no END before ELSEIF
elseif
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!