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?

 Accepted Answer

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.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!