MATLAB Answers

How to write this for loop as a while

52 ビュー (過去 30 日間)
N/A
N/A 2021 年 2 月 24 日
編集済み: N/A 2021 年 2 月 24 日
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 件のコメント
N/A
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 日
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 件のコメント
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by