필터 지우기
필터 지우기

how do I convert this to a for loop

조회 수: 1 (최근 30일)
Nicholas Repko
Nicholas Repko 2020년 4월 14일
답변: Peng Li 2020년 4월 14일
x = input('Enter a number between 1 and 100: ');
i = 0;
while x>0
x=x-i;
i=i+1;
end
fprintf('The while loop ran for %d interations\n' ,i)

채택된 답변

Peng Li
Peng Li 2020년 4월 14일
% your while loop
x = input('Enter a number between 1 and 100: ');
i = 0;
while x>0
x=x-i;
i=i+1;
end
fprintf('The while loop ran for %d interations\n' ,i)
% my for loop
x = input('Enter a number between 1 and 100: ');
for i = 0:100
x = x - i;
if x <= 0
break;
end
end
fprintf('The while loop ran for %d interations\n', i + 1)
Enter a number between 1 and 100: 30
The while loop ran for 9 interations
Enter a number between 1 and 100: 30
The while loop ran for 9 interations

추가 답변 (1개)

David Hill
David Hill 2020년 4월 14일
Don't understand the purpose of the loop.
x = input('Enter a number between 1 and 100: ');
for i=1:x
end
fprintf('The for loop ran for %d interations\n' ,x)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by