why is while loop used here?

조회 수: 1 (최근 30일)
Jack
Jack 2013년 9월 11일
hello, I'm trying to understand why are we using while in this situation? what is it originally asking for? I don't understand he we got the i to be 3 and a to be 46 in the final answer
x = [1:4];
y = x.^2;
z = x'*y;
a = 1;
while a<15
for i=1:3
a = a*x(i) + y(i);
if a<4
a = a + 2;
end
end
a = a + 1;
end
a = 46
i= 3

채택된 답변

Walter Roberson
Walter Roberson 2013년 9월 11일
"while" is being used because someone wanted to write obscure code, possibly for teaching purposes.
  댓글 수: 2
Jack
Jack 2013년 9월 11일
can you please comment on how we got the i and a?
Walter Roberson
Walter Roberson 2013년 9월 11일
It looks to me like it is intended to be an exercise. Follow the logic through step by step, working on paper if you must. (I followed the logic through in my head and confirmed that the code will produce those answers.)
You could also learn to use the debugger, and let it execute the program step by step and watch the variables change.

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

추가 답변 (0개)

카테고리

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