Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab (While loop) While loop with branching Write a while loop that adjusts us

ID: 3860036 • Letter: M

Question

Matlab (While loop)

While loop with branching Write a while loop that adjusts userValue while userValue is less than 0 or greater than 80. If userValue is greater than 80, then subtract 5 from userValue. If userValue is less than 0, then add 10 to userValue. 5 from userValue. If Your Function E Save CReset MATLAB Documentation 1 function userValue = Adjustvalue(uservalue) % Write a while loop that adjusts a uservalue while uservalue is less than 0 % or greater than 80 uservalue = 0; 4 5 % If the uservalue is greater than 80, then subtract 5 from uservalue 7 If the user value is less than 0, then add 10 to uservalue 9 10 end

Explanation / Answer

AdjustValue.m file

function userValue = AdjustValue(userValue)

% while true, if number < 0 increase and if number > 80 decrease else break
while true
if userValue<0
userValue = userValue + 10;
else if userValue > 80
userValue = userValue - 5;
else
break
end
end
end

main.m file

disp(AdjustValue(-6));
disp(AdjustValue(12));
disp(AdjustValue(96));

% output

% 4

% 12

% 76