Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Simple question loop for

1 回表示 (過去 30 日間)
Laurie
Laurie 2020 年 10 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello,
It's a very basic question because I'm new in the world of Matlab.
I want to know what happend when there is a for loop like this :
for i = 1:n
% Inside of the loop
end
but n = 0
Does it enter the loop or not at all ?
Thank you,
Laurie

回答 (2 件)

KSSV
KSSV 2020 年 10 月 19 日
編集済み: KSSV 2020 年 10 月 19 日
Lear debugging code. It will be help ful for you and easy.
If n = 0, it will not enter the loop.
n = 0 ;
for i = 1:n
fprintf("%d\n",i) % this will print index i to verify
end

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 10 月 19 日
You can simply add a disp command to see that nothing will be printed when n=0. It means that the statements inside the loop will not be executed.
n=0;
for i = 1:n
% test
disp(i);
% Inside of the loop
end
  1 件のコメント
Laurie
Laurie 2020 年 10 月 19 日
Thank you very much for your reply !

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by