While loop doesn't terminate

1 view (last 30 days)
Cody Nhem
Cody Nhem on 15 Oct 2019
Answered: Walter Roberson on 15 Oct 2019
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
Rik on 15 Oct 2019
You can find guidelines for posting homework on this forum here.
For this specific question, I suggest using the debugger to step through your code line by line.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 15 Oct 2019

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!