How many times will this while loop code run

How many times does the following code run?
total = 0;
while (total < 100)
total = total + 5;
disp(total)
end;
I thought it was 20, but I am wrong?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Mzo. de 2017

0 votos

It depends on whether you are counting the number of times the "while" statement executes or the number of times it will print a number. It will print 20 numbers, 5:5:100 . But the total<100 will execute one extra time to determine that the loop has ended.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de Mzo. de 2017

Comentada:

Jan
el 9 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by