0% found this document useful (0 votes)
127 views

3) Data Types, Operators and Control Statements

This document discusses data types, operators, and control statements in MATLAB. It introduces various integer data types like int8, uint8, int16 etc. and their ranges. It describes three types of operators - relational, logical, and arithmetic. It also explains various flow control statements like if-else, switch-case, while, for, continue, break, try-catch and provides examples of each. The overall objective is to introduce fundamental concepts in MATLAB programming.

Uploaded by

rajanikanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

3) Data Types, Operators and Control Statements

This document discusses data types, operators, and control statements in MATLAB. It introduces various integer data types like int8, uint8, int16 etc. and their ranges. It describes three types of operators - relational, logical, and arithmetic. It also explains various flow control statements like if-else, switch-case, while, for, continue, break, try-catch and provides examples of each. The overall objective is to introduce fundamental concepts in MATLAB programming.

Uploaded by

rajanikanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Types, Operators and

Control Statements

By
Dr. R. R. Manza
Objectives
• Introduction
• Integer Data Types
• Operators
• Flow Control Statements
• Examples
• Summary

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 2


Introduction
• There are many fundamental data types or classes including user
defined classes, object oriented classes and java classes in the
MATLAB. Each of these is in the form of an array. This array is a
minimum 0-by-0 in size. The two-dimensional versions of these
arrays are called matrices. We can build matrices and arrays of
floating point and integer data, characters and strings, logical true
and false states etc. User defined classed are used to develop our
own data type. The structure and cell array can store the dissimilar
type of data. Following are the data types;

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 3


Cont..
• Logical • int16 • User defined classes

• char • uint16 and function

• numeric • int32 handle.

• cell • uint32

• single • int64

• double • uint64

• int8 • Structures

• uint8 • java classes

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 4


Cont..

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 5


Integer Data Types and their Ranges
• Table : Integer Data Types and their Ranges:

Conversion Description Range of Values


function and
Data Type

int8 Signed 8-bit integer -128 to 127


uint8 Unsigned 8-bit integer 0 to 255
int16 Signed 16-bit integer -215 to 215 - 1
uint16 Unsigned 16-bit integer 0 to 216 - 1
int32 Signed 32-bit integer -231 to 231 - 1
uint32 Unsigned 32-bit integer 0 to 232 - 1

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 6


Operators
• There are three fundamental types of operators in MATLAB
programming:

1) Relational Operators

2) Logical Operators

3) Arithmetic Operators

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 7


Cont..
1) Relational Operators:
Operation: MATLAB command:
Strictly less than <
Less than or equal to <=
Strictly greater than >
Greater than or equal to >=
Equal to ==
Not equal to ~=

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 8


Cont..
2) Logical Operators:
Logical Operator MATLAB command
Logical AND &
Logical OR |
Logical NOT ~

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 9


Cont..
3) Arithmetic Operators:
Sr. Operator MATLAB Sr. Operator MATLAB
No Comman No Comman
d d
1 Addition 8 Power
+ .^
2 Subtraction
- 9 Transpose
.‘
3 Matrix Multiplication
* 10 Complex Conjugate
Transpose

4 Matrix Right
Division
/ 11 Multiplication
.*
5 Matrix Left Division 12 Division
\ ./
6 Colon Operator 13 Left Division
: \
7 Matrix Power
^
5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 10
Flow Control Statements
• There are eight flow-control statements in MATLAB:
1) if (with else and else if)
2) switch-case
3) while
4) for
5) continue
6) break
7) try….catch
8) return

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 11


Examples
1) if (with else and else if)
• Syntax • Example
N = input('Give numerator: ');
If (logical condition)
D = input('Give denominator: ');
Commands if D==0
elseif (logical condition) 'Sorry, cannot divide by zero'
commands else
else ratio = N/D
commands disp(ratio);
end end

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 12


Examples
2) Switch-case
• Syntax • Example
x = ceil(10*rand); % Generate a
switch expression
% random integer in {1, 2, ... , 10}
case value1
commands switch x
case value2 case {1,2}
commands disp('Probability = 20%');
. case {3,4,5}
. disp('Probability = 30%');
. otherwise
otherwise disp('Probability = 50%');
commands
end
end

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 13


Examples
3) While
• Syntax • Example

while expression
q = pi;
Commands while q >= 0.01
end q = q/2
end
• Output
q
q =0.0061

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 14


Examples
4) For Loop
• Syntax • Output
x
For index= start: increment: end
Commands x=
end Columns 1 through 8
0 0.5878 0.9511 0.9511
0.5878 0.0000 -0.5878 -0.9511
• Example
for n = 0:10 Columns 9 through 11
x(n+1) = sin(pi*n/5); -0.9511 -0.5878 -0.0000
end

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 15


Examples
5) Continue
• Example
x=-10;
while x<0
x=x+2;
if x = = -2
continue;
end
end

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 16


Examples
6) Break
• Example
x=-10;
while x<0
x=x+2;
if x = = -2
break;
end
end

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 17


Examples
7) Try…Catch
• Syntax • Example
try tryX = A * B
commands catch
catch errmsg = lasterr;
commands if(strfind(errmsg, 'Inner matrix
end dimensions'))
disp('** Wrong dimensions for
matrix multiply')
end

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 18


Examples
8) Return
• Example
if x == 0
m = 0;
r = 0;
return
end
u = log10(x)/log10(4);
if u < 0
m = floor(u)
else
m = ceil(u);
end
r = x/4^m;
5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 19
Summary
A function handle of MATLAB is used to hold information to be
used in referencing a function. There are three fundamental types of
operators in MATLAB programming: Relational Operators, Logical
Operators, and Arithmetic Operators.

5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 20


5/16/2013 Dept of CS & IT Dr. BAMU Aurangabad 21

You might also like