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?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Mär. 2017

0 Stimmen

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.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by