parse error on this function

조회 수: 1 (최근 30일)
Pengzhan Lu
Pengzhan Lu 2016년 11월 15일
답변: Jan 2016년 11월 15일
I want to calculate the sum of all the integers between (including) the lower and upper limits by using for loop. here is my code.
LL=input('Please enter an integer as lower limit.\n');
UL=input('Please enter an integer as upper limit.\n');
sum=0;
i=LL;
for i<=UL
sum=sum+i;
i=i+1;
end
I don't know why there is a parse error at '<='. can someone help me?

답변 (2개)

Walter Roberson
Walter Roberson 2016년 11월 15일
for i = LL : UL
or
while i <= UL

Jan
Jan 2016년 11월 15일
Please read the "Getting Started" chapters of the documentation and the help text of "for":
help for
Then:
LL = input('Please enter an integer as lower limit.\n');
UL = input('Please enter an integer as upper limit.\n');
S = 0; % Do not shadow the built-in function "sum"
for i = LL:UL
...
end
Do not increase "i" manually, because this is done by for already. If left the summation of S up to you, such that you can at least provide this solution whithout cheating - assumed that this is a homework.

카테고리

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