Lab 3
Lab 3
3.2 Introduction
In the previous labs, we introduced the fundamental concepts of computer programming, as well as
the basics of the MATLAB software package. Computer programming not only involves executing
several commands but controlling the sequence of execution (or control flow) using special
programming constructs. In this lab we introduce some of the programming constructs provided by
MATLAB.
The normal flow of control in a MATLAB script, and procedural programming in general, is sequential,
i.e. each program statement is executed in sequence, one after the other. This is illustrated in Fig. 3.1.
In computer programming, if all our programs featured only sequential control flow they would be
limited in their power. To write more complex and powerful programs we need to make use of
programming constructs that alter this normal control flow. One type of programming construct is the
conditional statement. The control flow of a conditional statement is illustrated in Fig. 3.2. In this
example there are two possible paths through the program, involving execution of different
statements. Which path is taken depends on the result of applying a test condition.
MATLAB if statements are one way of achieving a conditional control flow. The use of an if statement
is illustrated in the example below.
Remember if you like to put commands or make your program in .m file than just write than name of
saved file in command line to execute the program. On the other hand, if you like enter all commands
in command line before running any command use Shift+ Enter between each statements.
3.2.1 Example-1
a = input('Enter a number:');
if (a >= 0)
root = sqrt(a);
else
end
3.2.2 Example-2
r = rem( a , b )
if(r ==0)
disp('Number is Even')
else
disp('Number is Odd')
end
Example 3.2.1 and 3.2.2 introduced the concept of a condition. Conditions commonly involve
comparisons between expressions involving variables and values. For example, we saw the > and <
comparison operator in the example. A list of common comparison (or relational) operators such as >
is shown in Table 3.1, along with common logical operators for combining the results of different
comparisons.
Table 3.1 Common relational and logical operators
Most of these operators are intuitive. However, note the distinction between & and && (and likewise
between | and ||). The & and | operators perform AND and OR operations respectively. They will
evaluate the expressions on both sides of the operator and return a true or false value depending on
whether both (&) or (|) of them evaluated to true.
These operators will work with either scalar (i.e. single value) logical expressions or array expressions
(i.e. that evaluate to an array of true/false values). The only restriction is that MATLAB must be able
to match the expressions on either side of the operator. Either both should be scalars, both should be
arrays of the same size, and one should be an array and the other a scalar.
The && and || operators perform the same AND and OR operations but using what is known as a
short-circuiting behavior. This means that, if the result of the overall AND/OR operation can be
determined from the left-hand expression alone, then the right-hand expression will not be evaluated.
For example, if the left-hand expression of an AND operation is false then the result of the AND will
also be false, regardless of the value of the right-hand expression. Therefore, the advantage of short-
circuiting is that unnecessary operations are not performed. However, note that && and || can only
be used with scalar values, not arrays. The use of these relational and logical operators is illustrated
in the following code excerpt.
3.3.1 Example
end
end
Here, in both if statements, the Boolean variable is set to true only if both comparisons evaluate to
true. If either comparison evaluates to false the flow of execution will pass to after the corresponding
end statement. We can use the short-circuiting version of the AND operator as both sides of the
operator are scalar (logical) values (i.e. not arrays).
There are two different operators in this condition: && and >. In what order would MATLAB evaluate
them? The answer to this question lies in the rules for operator precedence. Whenever MATLAB needs
to evaluate an expression containing multiple operators it will use the order of precedence shown in
Table 3.2. We have come across all these operators before with a few exceptions. The ' operator, when
placed after a matrix, is just a short way of calling the transpose function, which we introduced earlier.
The unary plus and minus operators refer to a + or - sign being placed before a value or variable to
indicate its sign
Now let’s return to our condition without brackets. Of the two operators present (&& and >), we can
see from Table 3.2 that the ones with the highest precedence are >and &&.
In this example, removing the brackets still resulted in the desired behavior. However, this will not
always be the case. Furthermore, even if the brackets are not essential it is still a good idea to include
them to make our code easier to understand and less prone to errors.
3.4 Tasks
3.4.1 Cataract is an eye disease which forms cloudy area in the eye lens. Write a program in
Matlab using conditional statements to show the severity of this disease. Input will be taken
by user in numerical form. The Output should be in the form of Low, normal, and High in
range. The ranges are given below:
Code
if C<=70
fprintf('Low \n')
fprintf('Normal \n')
elseif C>90
fprintf('High \n')
end
Output
Figure 3.3
Age<30 No chance Age=30 or <50 slighter chances Age=50 or >50 High chances
Code
S1=input('Press 1 if you have cough that lasts more than three weeks otherwise enter any other
number \n');
S2=input('Press 2 if you have Loss of appetite and unintentional weight loss otherwise enter any
other number \n');
S3=input('Press 3 if you have Fever,Chills,Night sweats otherwise enter any other number \n');
fprintf('From age point of view you have no chance but your symptoms show you have TB \n')
fprintf('From age point of view you have no chance but you have 1 symptom of TB \n')
fprintf('From age point of view you have no chance but you have 1 symptom of TB \n')
fprintf('From age point of view you have no chance but you have 2 symptoms of TB \n')
fprintf('From age point of view you have no chance but you have 2 symptoms of TB \n')
fprintf('From age point of view you have no chance but you have 2 symptoms of TB \n')
fprintf('From age point of view you have no chance and also your symptoms show you have no
TB \n')
fprintf('From age point of view you have slighter chances but your symptoms show you have TB
\n')
fprintf('From age point of view you have slighter chances and also you show 1 symptom of TB \n')
fprintf('From age point of view you have slighter chances and also you show 1 symptom of TB \n')
fprintf('From age point of view you have slighter chances and also you show 1 symptom of TB \n')
fprintf('From age point of view you have slighter chances and also you show 2 symptoms of TB
\n')
fprintf('From age point of view you have slighter chances and also you show 2 symptoms of TB
\n')
fprintf('From age point of view you have slighter chances and also you show 2 symptoms of TB
\n')
fprintf('From age point of view you have slighter chances but your symptoms show you have no
TB \n')
elseif A>=50 && S1==1 && S2==2 && S3==3
fprintf('From age point of view you have higher chances and also your symptoms show you have
TB \n')
fprintf('From age point of view you have higher chances and also you show 1 symptom of TB \n')
fprintf('From age point of view you have higher chances and also you show 1 symptom of TB \n')
fprintf('From age point of view you have higher chances and also you show 1 symptom of TB \n')
fprintf('From age point of view you have higher chances and also you show 2 symptoms of TB \n')
fprintf('From age point of view you have higher chances and also you show 2 symptoms of TB \n')
fprintf('From age point of view you have higher chances and also you show 2 symptoms of TB \n')
fprintf('From age point of view you have higher chances but your symptoms show you have no TB
\n')
end
Output
Figure 3.4
Figure 3.5
3.4.3 If a=1, b=2 and c=3, use your knowledge of the rules of operator precedence to predict
the values of the following expressions (i.e. work them out in your head and write down the
answers). Verify your predictions by evaluating them using MATLAB.
1. a+b ∗ c
2. a ^ b+c
3. 2 ∗ a == 2 && c - 1 == b
4. b + 1:6
Output
Figure 3.6
Figure 3.7
3.4.4 Body mass index (BMI) can be found, according to the formula based on a height (in
meters) and weight (in kilograms) entered using the keyboard.
𝐵𝑀𝐼 = 𝑚𝑎𝑠𝑠/ℎ𝑒𝑖𝑔ℎ𝑡 2
Write a Matlab program which will be able to display BMI with type of body of a person after
calculating it using the above stated formula.
Code
BMI=W/H^2;
if BMI<=18.5
elseif BMI>30
end
Output