Use of semicolon in a for loop
Mostrar comentarios más antiguos
Hello,
I think I once read somewhere that using a semicolon in a for loop statement i.e.
for i = 1:N; %With a semicolon
my_function
end
instead of
for i = 1:N %No semicolon
my_function
end
was better because the 1:N was not created. However I can't seem to be able to find anything about it anymore. Could someone tell if it's true or not or if not what's the point of using a semicolon?
Respuesta aceptada
Más respuestas (1)
Daniel Shub
el 19 de Dic. de 2011
I don't think a semicolon or comma adds anything. It does allow everything to be written on one line like
for i = 1:N; my_function, end
Outside of the command window, I am not sure why you would want to do this.
4 comentarios
Walter Roberson
el 19 de Dic. de 2011
All on one line like that can have notably different timing than each on separate lines, at least prior to 2010b (I think it was.) Slower on one line, I think it was.
Daniel Shub
el 19 de Dic. de 2011
I also believe it is slower. I am pretty sure if it was faster, my code would look a lot different. Imagine how unreadable code would be if it all had to be on one line.
Côme
el 19 de Dic. de 2011
Daniel Shub
el 19 de Dic. de 2011
No, you don't need it, but sometimes it is easier. I find tab completion and the history behaves better if it is all on one line.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!