MATLAB: For loops – Textbook Example

break statements

I have the following code from a textbook exercise which asks me to arrive at the value of ires; the answer key has ires=25 as the answer. After struggling with the exercise, I think I have the logic down, but I need this additional clarification. It is my understanding that after MATLAB encounters the break statement, it stops the inner loop and goes to the next statement after the end of the inner loop; in this case, the "next statement" is ires=ires+1. My question is this: am I correct in assuming that once index2==6, ires=ires+1 doesn't run (ie. ires doesn't get incremented)?
ires=0;
for index1=1:10
for index2=index1:10
if index2==6
break;
end
ires=ires+1;
end
end

Best Answer

When the condition is met, Matlab will exist the loop, which means ires will not be incremented. All instructions after break, inside the for loop will be skiped
Related Question