For loop code not working right?

Question: Set the variable p9 to equal 7. Then run a for loop which goes through the numbers n = 0, 3, 6, 9, ..., 300 and adds them each in turn to p9 (changing it each time).
Code:
p9=7;
for p9=[0:3:300];
disp(num2str(p9));
p9=p9+n;
end;
This is giving me the wrong answer and I am not sure why? Thanks for your help!

1 件のコメント

Stephen23
Stephen23 2015 年 2 月 9 日
disp works just fine on numeric data, you don't need the num2str.

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

回答 (2 件)

James Tursa
James Tursa 2015 年 2 月 9 日

1 投票

You have used the variable p9 as your loop index AND as the accumulation variable. Use a different variable name for the loop index, e.g. use k=0:3:300 instead of p9=0:3:300. Then you can add k to p9 in your loop (instead of n ... where does n come from?).

8 件のコメント

Bob
Bob 2015 年 2 月 9 日
編集済み: James Tursa 2015 年 2 月 9 日
So Now I have this code:
p9=7;
for k=[0:3:300]
disp(num2str(p9))
p9=k+p9
end
And it is going all the way up to 15157, when I want it to stop once it gets to 300. Why?
Stephen23
Stephen23 2015 年 2 月 9 日
編集済み: Stephen23 2015 年 2 月 9 日
Your variable n is defined to go from 0 to 300 in steps of three. You have variable p9 defined as being the cumulative sum of itself starting from p9=7, plus the loop iterator variable n in each step. This is clearly going to be a much bigger value than 300. And that is what you are displaying in your loop.
Try displaying the n variable, and you will see that it goes up to 300, just like it should:
p9 = 7;
for n = 0:3:300
disp(n)
p9 = n + p9,
end
Bob
Bob 2015 年 2 月 9 日
Ok but the way I read the question is it should stop at 300, so how would I do that?
Stephen23
Stephen23 2015 年 2 月 9 日
編集済み: Stephen23 2015 年 2 月 9 日
The loop variable does stop at 300, just like was asked.
"Then run a for loop which goes through the numbers n = 0, 3, 6, 9, ..., 300 and adds them each in turn to p9" states quite clearly that the loop variable n should go from 0 to 300. How would you interpret that? What should stop at 300? p9 or the loop variable n?
Bob
Bob 2015 年 2 月 9 日
I would think p9 should stop at 300
James Tursa
James Tursa 2015 年 2 月 9 日
編集済み: James Tursa 2015 年 2 月 9 日
No. The loop index "k" (or Stephen uses "n") stops at 300. The accumulated sum variable "p9" will end up being 7 + 0 + 3 + ... + 300.
Bob
Bob 2015 年 2 月 9 日
My teacher said it should stop when n=300. So I think I am done?
James Tursa
James Tursa 2015 年 2 月 9 日
If you understand the code, then I think you are done also.

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

Image Analyst
Image Analyst 2015 年 2 月 9 日

0 投票

In a for loop, if you change the loop iterator variable, your "p9", inside the loop, it will have that value for the remainder of that iteration of the loop. However, once it hits the end and start the next iteration of the loop, it will have the value that it would have had, had you not changed it - it will not have anything to do with what value you changed it to.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

Bob
2015 年 2 月 9 日

コメント済み:

2015 年 2 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by