Originally Posted by

**roryman987** I need help understanding these two questions. I have managed to do all of my assignments in Matlab without using/understanding these type of problems but they are on my midterm review. I have the answers but can't seem to grasp how to arrive at them. I understand how to use while and for loops in the codes that I have written, but can't figure out the answers to these questions. Help with either one would be greatly appreciated!

Question #1

The output of the following code is? Answer is sum=18

n=0;

sum=0;

while n<10

sum=sum+n;

n= n+3;

end

fprintf('sum= %d \n', sum)

Look at the following trace table for the while loop:

Code:

n_ on_entry sum n_on_exit
0 0 3
3 3 6
6 9 9
9 12 12
12 - end loop