Clear Filters
Clear Filters

Replacing while loop with break command

3 views (last 30 days)
Need help rewriting the following MATLAB code using a while loop, to avoid using the break command.
clc;clear;close all;
limit=210;
m=100;
sum=0;
for i=1:m
sum=sum+i;
if sum>limit
break
end
end
sum
n=i

Accepted Answer

David Sanchez
David Sanchez on 11 Jun 2013
clc;clear;close all;
limit=210;
k = 0;
sum = 0;
while sum <= limit
k=k+1;
sum = sum + k;
end
sum
n=k

More Answers (0)

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!