0% found this document useful (0 votes)
31 views24 pages

ENGFF012: Lesson 6: If Statement

A(r,c)<0 end y=A;

Uploaded by

Ryan Loh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views24 pages

ENGFF012: Lesson 6: If Statement

A(r,c)<0 end y=A;

Uploaded by

Ryan Loh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1

ENGFF012
Lesson 6: if statement
2

Learning Objectives
At the end of session you should be able to:
• Write simple MATLAB programs using if statements
• Use of nested loop
3

Relational Operators
Operator Operations

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

== Equal to

~= Not equal to
4

Logical Operators
Operator Operations

& Logical AND

&& Logical AND with shortcut evaluation

| Logical OR

|| Logical OR with shortcut evaluation

~ Logical NOT
5

Short-circuit logical operations: && ||


expression1 && expression2
expression1 && expression2 represents a logical AND operation that
employs short-circuiting behaviour. That is, expression2 is not
evaluated if expression1 is logical 0 (false).
For example:
1==2 && 5>0 1==2 & 5>0
False True
False
Result: False Result: False

expression1 || expression2
expression1 || expression2 represents a logical OR operation that
employs short-circuiting behaviour. That is, expression2 is not
evaluated if expression1 is logical 1 (true).
For example:
5>0 || 1==2 5>0 | 1==2
True True False
Result: True Result: True
Reference: http://www.mathworks.com/help/matlab/ref/logicaloperatorsshortcircuit.html#bt_0nme
6

Relational and Logic Operators


Relational and logic operators are operators that produce a
true (the value 1) or false (the value 0) result

Operation Result
3<4
3==4
1+2~=3
2<3 & 0>=7
2<3 || 0>7
a=2;
b=0;
~a
~b
7

Relational and Logic Operators


Relational and logic operators are operators that produce a
true (the value 1) or false (the value 0) result

Operation Result
3<4 1
3==4 0
1+2~=3 0
2<3 & 0>=7 0
2<3 || 0>7 1
a=2;
b=0;
~a 0
~b 1
8

The if construct has the form


If control_expr_1
Statement 1
Statement 2 Block 1


elseif control_expr_2
Statement 1
Statement 2 Block 2

else
Statement 1
Statement 2 Block 3

end
9

If today is sunny

elseif today is snowing

else today is raining

end
10

Testing
Example >> checkvalue
Enter a value:5
5 is a positive number
% Script file: checkvalue.m >> checkvalue
% Prompt the user for a value Enter a value:-10
-10 is a negative number
x = input('Enter a value:'); >> checkvalue
Enter a value:0
0 is a zero
% Perform calculations and display
if x>0
fprintf('%d is a positive number\n',x);
elseif x<0
fprintf('%d is a negative number\n',x);
else
fprintf('%d is a zero\n',x);
end
11

Exercise
Write a MATLAB program to evaluate an input of score. If
score is greater or equal to 40, display PASS. If score is
less than 40, display FAIL.
12

Solution
% Script file: myscore.m
% Prompt the user for the score
score = input('Enter the value of score:');

% Perform calculations and display the result


if score>=40 Testing
>> myscore
fprintf('PASS\n'); Enter the value of score:37
else FAIL
>> myscore
fprintf('FAIL\n'); Enter the value of score:40
end PASS
>> myscore
Enter the value of score:85
PASS
13

Exercise: Function of Two Variables


Write a MATLAB program to evaluate a function 𝑓 𝑥, 𝑦 for any two user-
specified values of x and y. The function 𝑓 𝑥, 𝑦 is defined as follows:

𝑥+𝑦 𝑥 ≥ 0 𝑎𝑛𝑑 𝑦 ≥ 0
𝑥 + 𝑦2 𝑥 ≥ 0 𝑎𝑛𝑑 𝑦 < 0
𝑓 𝑥, 𝑦 =
𝑥2 + 𝑦 𝑥 < 0 𝑎𝑛𝑑 𝑦 ≥ 0
𝑥2 + 𝑦2 𝑥 < 0 𝑎𝑛𝑑 𝑦 < 0

The function 𝑓 𝑥, 𝑦 is evaluated differently depending on the signs of the two


independent variables x and y.
14

Solution
% Script file: funxy.m
% Prompt the value for the values x and y
x=input('Enter the x coefficient:');
y=input('Enter the y coefficient:'); Control Expression
%calculate the function f(x,y)
if x>=0 && y>=0
fun=x+y; 𝑥 + 𝑦 , 𝒙 ≥ 𝟎 𝒂𝒏𝒅 𝒚 ≥ 𝟎
elseif x>=0 && y<0
fun=x+y^2; 𝑥 + 𝑦 2 , 𝒙 ≥ 𝟎 𝒂𝒏𝒅 𝒚 < 𝟎
elseif x<0 && y>=0 𝑓 𝑥, 𝑦 =
𝑥 2 + 𝑦 , 𝒙 < 𝟎 𝒂𝒏𝒅 𝒚 ≥ 𝟎
fun=x^2+y;
else
𝑥 2 + 𝑦 2 , 𝒙 < 𝟎 𝒂𝒏𝒅 𝒚 < 𝟎
fun=x^2+y^2;
end
fprintf('The value of the function is %f\n',fun);
15

Testing
>> funxy
Enter the x coefficient:5
Enter the y coefficient:2
The value of the function is 7.000000
>> funxy
Enter the x coefficient:-4
Enter the y coefficient:2
The value of the function is 18.000000
>> funxy
Enter the x coefficient:2
Enter the y coefficient:-3
The value of the function is 11.000000
>> funxy
Enter the x coefficient:-2
Enter the y coefficient:-3
The value of the function is 13.000000
16

Exercise
Write a function that calculates the factorial function. The factorial function is
defined as

1 𝑛=0
𝑛! = ቊ
𝑛 × 𝑛 − 1 × 𝑛 − 2 …× 2 × 1 𝑛>0

Note: Write the MATLAB code to calculate N factorial for positive value of N
17

Solution 𝑛! = ቊ
1 𝑛=0
% Script file: myfactorial.m 𝑛 × 𝑛 − 1 × 𝑛 − 2 …× 2 × 1 𝑛>0
% Prompt the user for the n
n = input('Enter the value of N:');

% Perform calculations and display the result


if n==0
fprintf('The resulting value of %d! is %d\n',n,1);
elseif n>0
p=1;
for i=1:n
p=p*i;
p=factorial(n);
end;
fprintf('The resulting value of %d! is %d\n',n,p);
else
fprintf('n must be a positive value\n');
end
18

Testing
>> myfactorial
Enter the value of N:3
The resulting value of 3! is 6
>> myfactorial
Enter the value of N:5
The resulting value of 5! is 120
>> myfactorial
Enter the value of N:0
The resulting value of 0! is 1
>> myfactorial
Enter the value of N:4
The resulting value of 4! is 24
19

Example: If statement & Nested Loop


You are given a matrix. Write a function
that changes all negative numbers to zero.
For example:

3 6 1  9 0 6 1 0
   
 4 8 1 2  4 8 1 2
 5  2 7  5 is 5 0 7 0
  changed  
 2 6 4 1 2 6 4 0
 3  to: 3 8 
 4  3 8   4 0
20

Solution
function[y]=matrixchange(A)
[row, column]=size(A);
for r=1:row
for c=1:column Outer loop
st loop: r = 1 A(1,1) A(1,2) A(1,3) A(1,4)
if A(r,c)<0 1
Inner loop: c = 1, 2, 3, 4
A(r,c)=0;
2nd loop: r = 2 A(2,1) A(2,2) A(2,3) A(2,4)
end Inner loop: c = 1, 2, 3, 4
end 3rd loop: r = 3 A(3,1) A(3,2) A(3,3) A(3,4)
end Inner loop: c = 1, 2, 3, 4
y=A; 4th loop: r = 4
A(4,1) A(4,2) A(4,3) A(4,4)
end th
Inner loop: c = 1, 2, 3, 4
5 loop: r = 5
A(5,1) A(5,2) A(5,3) A(5,4)
Inner loop: c = 1, 2, 3, 4
21

Testing
>> a=[1 2 3;5 6 7;-2 -3 -7;4 -9 0]
a=

1 2 3
5 6 7
-2 -3 -7
4 -9 0

>> matrixchange(a)
ans =

1 2 3
5 6 7
0 0 0
4 0 0
22

Exercise
You are given a matrix. Write a function
that changes all positive numbers to 10
and all negative numbers to 0.
For example:
3 6 1  9 0 10 10 0 
   
 4 8 1 2  10 10 10 10 
 5  2 7  5 is 10 0 10 0 
  changed  
 2 6 4 1 10 10 10 0 
 3  to: 10 0 10 
 4  3 8   10
23

Solution
function[y]=matrixchange(A)
[row, column]=size(A);
for r=1:row
for c=1:column
if A(r,c)<0
A(r,c)=0;
elseif A(r,c)>0
A(r,c)=10;
end
end
end
y=A;
end
24

Summary
• Write simple MATLAB programs using if statements
if …
elseif …
else …
end
• Use of nested loop
for…
for…
end
end

You might also like