While loop doesn't terminate

조회 수: 1 (최근 30일)
Cody Nhem
Cody Nhem 2019년 10월 15일
답변: Walter Roberson 2019년 10월 15일
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
Rik
Rik 2019년 10월 15일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2019년 10월 15일

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by