why am I having zero as the answer in the code below?

1 回表示 (過去 30 日間)
andrew brown
andrew brown 2021 年 3 月 9 日
コメント済み: andrew brown 2021 年 3 月 9 日
why am I having zero as the answer in the code below?
a = 0
for i = 1:0.5:2
for j = 1:1:3
a = a + j;
end
end

採用された回答

Nathanael Jenkins
Nathanael Jenkins 2021 年 3 月 9 日
The only output from that code will be where you define a at the start of the program (without a semicolon at the end).
Your code is calculating correctly, but it's not displaying the final value of a.
You need to add a line after the loops to print the final value of a:
a = 0;
for i = 1:0.5:2
for j = 1:1:3
a = a + j;
end
end
disp(a)
  1 件のコメント
andrew brown
andrew brown 2021 年 3 月 9 日
Thanks very much, very helpful

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by