While loop doesn't terminate
1 view (last 30 days)
Show older comments
In an attempt to have Matlab compute the sum S = 0.1 + 0.2 + . . . + 0.9, someone writes the following code:
s = 0
x = 0
while x~=1.0
s = s + x
x = x + 0.1
end
S = s
(a) Test this code on Matlab. Why does the program keep running indefinitely? Note: to terminate the procedure, place the cursor in the command window and press Ctrl + C.
(b) What should be changed in the code to make it stop?
1 Comment
Rik
on 15 Oct 2019
For this specific question, I suggest using the debugger to step through your code line by line.
Answers (1)
See Also
Categories
Find more on MATLAB Compiler 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!