Why does my code only print the final iteration?
2 views (last 30 days)
Show older comments
function [ ] = forloopef(x , y, m, n)
while x < 49
[x] = 7 + x
end
while y < 49
[y] = 7 + y
end
while m > 7
[m] = m - 14
end
while n > 7
[n] = n - 14
end
hold on
rectangle('Position',[x y m n])
axis([0 100 0 100])
3 Comments
Guillaume
on 19 Mar 2017
Note 1: I would get out of the habit of writing brackets [] around the assignee in your code. Not only is it completely unnecessary, it is also likely to cause you problems in some future code.
x = 7 + x
works just as well.
Note 2: And of course none of the while loops are necessary:
x = max(x, mod(x, 7) + 49);
y = max(x, mod(y, 7) + 49);
m = min(m, mod(m + 6, 14) - 6;
n = min(m, mod(n + 6, 14) - 6;
Image Analyst
on 19 Mar 2017
Edited: Image Analyst
on 19 Mar 2017
Don't double space code to format it. Simply learn to format it. See the tutorial: http://www.mathworks.com/matlabcentral/answers/13205#answer_18099 Basically, paste in code, highlight it, and click the {}Code icon.

Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!