How to write this for loop as a while

Hi everyone,
I was wondering how I could rewrite this for loop as a while loop only using the variables y and k.
y = 0.0;
for k = 1:4:999
y = y + k*k;
end
Thanks!

댓글 수: 2

Rik
Rik 2021년 2월 24일
One deleted comment and a username change. These things make me suspicious. Before more is going to disappear: link.
N/A
N/A 2021년 2월 24일
편집: N/A 2021년 2월 24일
@Rik no need to be suspicious, I am NOT trying to gaslight anyone. I deleted the comment because I took your's the wrong way. Concerning the username change: yeah i get the look of that but I've changed it two times already today because i wasn't quite happy with it (generation zoomer be like)
On another note I do think it's fair to delete a comment before anyone has answered it just like it is allowed to do that with theads as a whole.

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

답변 (1개)

Rik
Rik 2021년 2월 24일

1 개 추천

Every for-loop can be re-written to a while-loop.
y = 0;
k = 1;
while k<=999
y = y + k*k;
k = k + 4;
end

댓글 수: 7

N/A
N/A 2021년 2월 24일
편집: N/A 2021년 2월 24일
Thanks so far, that's the solution I came up with as well. Is there another way to rewrite the for loop without changing the value of the variables?
What do you want to achieve? If you don't want to change the value of the variables you can do this:
y=0;k=1;
while false
end
But I doubt that is what you mean.
So why do you want to change the for to while, and what is the end goal you want to achieve?
N/A
N/A 2021년 2월 24일
편집: N/A 2021년 2월 24일
I have an exercise with a for loop and the variables k and y in front of me (the one in the question) and I have to rewrite it as a while loop. By "without changing the value" I mean the loop should work with these values:
y = 0.0;
k = 1:4:999;
Is that possible or do I have to initialize the variables k and y with other values to rewrite it as a while loop?
y = sum((1:4:999).^2);
k = 1;
while false % Completely useless...
end
N/A
N/A 2021년 2월 24일
Ok, since there are apparently no reasonable solutions that work with the initial values, the task probably only wants me to use two variables with the identifiers k and y and not take the original values from the for loop.
Thanks for the help to all.
Rik
Rik 2021년 2월 24일
You can find guidelines for posting homework on this forum here. At the very least you should have mentioned this is homework and have included the exact assignment text. You are not repeating all essential information, either because you don't understand Matlab well enough yet, or because you think it is a trivial detail.
Rik
Rik 2021년 2월 24일
In your now-deleted comment you mentioned the exact assignment text:
"What are the appropriate MATLAB statements if instead of a for loop a while loop is used? In doing so, use only the variables k and y."
My answer is the solution to that question, although, as you can see in Jan's comment, you don't need a loop at all.
Note that my previous comment is not meant as a slur at all. I am well aware nobody is born knowing every detail about Matlab or the mores of this forum. The first you are already solving by doing the course you're doing, the second I tried to remedy by telling you and linking you a thread.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

N/A
2021년 2월 24일

편집:

N/A
2021년 2월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by