MATLAB: How to sum values of (x plus x-i) for (i=1 to x-1) using a while loop

while loop

As a disclaimer, this is not homework. It is from an independent program which my instructor has given to anyone wanting additional MATLAB practice.
So here is my code
function summedValue = SummationWithLoop(userNum) % Summation of all values from 1 to userNum
summedValue = 0;
i = 1;
% Write a while loop that assigns summedValue with the
% sum of all values from 1 to userNum
while i<userNum
summedValue=userNum+(userNum-i)
i=i+1
end
What i want to do is input a value (say 5) and get the sum of (5+4+3+2+1=15) The pseudo code would look like the initial question above. Can you help correct this code I attempted to write?

Best Answer

sum(1:userNum); % better practice of MATLAB
and variant for your "dance":
summedValue = 0;
userNum = 5
while userNum ~= 0
summedValue = summedValue + userNum;
userNum = userNum - 1;
end