Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Simple question loop for

1 visualización (últimos 30 días)
Laurie
Laurie el 19 de Oct. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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

Respuestas (2)

KSSV
KSSV el 19 de Oct. de 2020
Editada: KSSV el 19 de Oct. de 2020
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 el 19 de Oct. de 2020
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 comentario
Laurie
Laurie el 19 de Oct. de 2020
Thank you very much for your reply !

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by