Nested if statements under for loop
Show older comments
I have this nested if statements under a for loop. I want the for loop to calculate 3.1*5^2. And if the answer is 77.5, it should solve 3.1*3^2. Also if that equals 27.9. It should stop the for loop. But it seems to continue. Please help.
clc
A=['3' '.' '1' '*' '^' '2']
for n=0:length(A)
try
d=[A(1:n) '5' A(n+1:end)]
c=eval(d)
if c==77.5
e=[A(1:n) '3' A(n+1:end)]
j=eval(e)
if j==27.9
return
end
end
end
end
1 Comment
Rik
on 8 Aug 2021
You should compare to a tolerance. (i.e. abs(value-target)<=tol;)
You should also remove those eval calls.
Accepted Answer
More Answers (0)
Categories
Find more on Language Support 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!