Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

if ( x >= 0 & x <= 1.

0 )

y = sin ( pi * x );
fprintf ( \n y = %f \n, y );

else

y = 3.0;
fprintf (\n constant level \n );

end

if Body_temp >= 102


fprintf('The patient has a high fever.\n')
elseif Body_temp >= 99
fprintf('The patient has a low grade fever.\n');
elseif Body_temp >= 96
fprintf('The patient has a normal body temperature.\n');
elseif Body_temp >= 92
fprintf('The patient has a low body temperature.\n');
else
fprintf('The patient is hypothermic.\n');
end

if Body_temp >= 102


fprintf('The patient has a high fever.\n')
elseif Body_temp > 98.6
fprintf('The patient has a low grade fever.\n');
elseif Body_temp < 95
fprintf('The patient has a low body temperature.\n');
end
for x=[1 2 3 4 5]
disp(num2str(2*x))
end

syms x;
for n=[2:7]
disp(['I will take the derivative of ',char(x^n)])
disp(['The derivative of ',char(x^n),' is ',char(diff(char(x^n),'x'))])
end

total = 0;
n=0;
while(total<1000)
n = n+1;
total = total + n;
end
disp(n)

x = ones(1,10);

for n = 2:6

x(n) = 2 * x(n - 1);

end

while statements loop as long as a condition remains true.

For example, find the first integer n for which factorial(n) is a 100-digit number:

n = 1;

nFactorial = 1;

while nFactorial < 1e100

n = n + 1;

nFactorial = nFactorial * n;
end

1. Make sure your if statements do not include redundant tests. This makes your code
more difficult to understand, harder to maintain over time, and slower to execute. The
following is an example of a poorly-designed if statement with 4 unnecessary redundant
tests.

if Score >= 90
disp('A')
elseif Score >= 80 & Score < 90
disp('B')
elseif Score >= 70 & Score < 80
disp('C')
elseif Score >= 60 & Score < 70
disp('D')
elseif Score < 60
disp('F')
end

A better design without redundant tests is:

if Score >= 90
disp('A')
elseif Score >= 80
disp('B')
elseif Score >= 70
disp('C')
elseif Score >= 60
disp('D')
else
disp('F')
end

You might also like