Professional Documents
Culture Documents
Chapter5-1-Exercise - Selection (Hossain)
Chapter5-1-Exercise - Selection (Hossain)
EXERCISE 5-1
Answer the following questions after checking the output of the codes in MATLAB.
a = 10;
if a ∼= 0
disp('a is not equal to 0')
end
Answer:
a is not equal to zero
a = 10;
if a > 0
disp('a is positive')
else
disp('a is not positive')
end
Answer:
a is positive
a = 5;
b = 3;
c = 2;
if a < b*c
disp('Hello world')
else
disp('Goodbye world')
end
ANSWER:
Hello world
4 Suppose the code in Problem 2 is modied by adding parentheses around a > 0. What will it
print?
a = 10;
if (a > 0)
disp('a is positive')
else
disp('a is not positive')
end
Answer:
a is positive
5. Suppose the code in Problem 3 is modied by adding the parentheses shown below. What will it
print?
a = 5;
b = 3;
c = 2;
if (a < b)*c
disp('Hello world')
else
disp('Goodbye world')
end
Answer:
Goodbye world
p1 = 3.14;
p2 = 3.14159;
if p1 == p2
disp('p1 and p2 are equal')
else
disp('p1 and p2 are not equal')
end
Answer:
p1 and p2 are not equal
7. What will the following code print?
a = 5;
b = 10;
if a == b
disp('a and b are equal')
else
disp('a and b are not equal')
end
Answer:
a and b are not equal
8. For what values of the variable a will the following MATLAB code print 'Hello world'?
if ∼ a == 0
disp('Hello world')
else
disp('Goodbye world')
end
Answer:
Any value as long as it is not zero.
9. For what values of the variable a will the following code print 'Hello world'?
Answer:
Any number between 0 and 6
10. For what values of the variable a will the following code print 'Hello world'?
if a < 3 || a > 10
disp('Hello world')
else
disp('Goodbye world')
end
Answer:
For any values below 3 and any values above 10.
11. For what values of the variable a will the following code print 'Hello world'?
if a < 7 || a >= 3
disp('Hello world')
else
disp('Goodbye world')
end
Answer:
Any value is possible
12. Write an if statement that will print 'a is very close to zero' if the value of the variable a is
between -0.01 and 0.01.
Answer: