Programming
Constructs in MATLAB ®
Susovan Jana
Department of Information Technology
Institute of Engineering & Management, Kolkata, INDIA
Email (O): [email protected]
Email (P): [email protected]
2
Input from User
Numeric Input from Command Window
─ prompt = 'Enter the value of X: ';
─ X = input(prompt);
─ disp(X)
─ disp(['You have entered ', num2str(X)]);
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
3
Input from User
String input from command window
─ prompt = 'Type your name here: ';
─ str = input(prompt,'s');
─ msgbox(['Your name is ',str]);
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
4
Conditional Statements
Conditional statements enable you to select at run
time which block of code to execute.
The statements are if, elseif or else, switch
The simplest conditional statement is an if statement.
Indent is not mandatory here
Syntax
if <condition>
<statements>
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
5
Example of if Statement
% Generate a random number
a = randi(100, 1);
fprintf("The value of a is:");
disp(a);
% if remainder is zero then a is even
if rem(a, 2) == 0
disp('a is even');
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
6
if-else Statement: Syntax
if <expression>
<statements>
else
<statements>
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
7
if, else Statement: Example
% Generate a random number
a = randi(100, 1);
fprintf("The value of a is:");
disp(a);
% if remainder is zero then a is even else odd
if rem(a, 2) == 0
disp('a is even');
else
disp('a is odd');
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
8
if, elseif, else Statement: Syntax
if <expression>
statements
elseif <expression>
statements
else
statements
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
9
if, elseif, else Statement: Example
marks=input('Enter your marks: ');
if marks >= 60
disp('First Division');
elseif marks >= 50
disp('Second Division');
elseif marks >= 40
disp('Third Division');
else
disp('Fail');
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
10
switch Statement: Syntax
switch <switch_expression>
case <case_expression>
statements
case <case_expression>
statements
...
otherwise
statements
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
11
switch Statement: Example 1
n = input('Enter a number: ');
switch n
case -1
disp('negative one')
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
12
switch Statement: Example 2
n = input('Enter a character: ','s');
switch n
case {'a', 'A'}
disp('Apple')
case {'b', 'B'}
disp('Ball')
case '1'
disp('One')
case '2'
disp('Two')
case '3'
disp('Three')
otherwise
disp('other value')
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
13
for Loop
Syntax
for index = values
statements
end
values
─ initVal:endVal
─ initVal:step:endVal
─ valArray — Create a column vector, index, from subsequent
columns of array valArray on each iteration. The loop executes a
maximum of n times, where n is the number of columns of
valArray, given by numel(valArray(1,:)). The input valArray can be
of any MATLAB® data type, including a character vector, cell
array, or struct.
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
14
for loop: Examples
Demo 1
for i=1:5
disp(i);
end
Demo 2
for i=1:2:5
disp(i);
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
15
for loop: Examples
Demo 3
for i=[7 4 3 9 11]
disp(i);
end
Demo 4
for i={‘a’ ‘b’ ‘c’}
disp(i);
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
16
Nested for loop: Syntax
for index = values
for index = values
statements
end
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
17
Nested for loop: Example
for i=1:5
for j=1:i
fprintf('*');
end
fprintf('\n');
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
18
while Loop
while expression, statements, end evaluates an
expression, and repeats the execution of a group of
statements in a loop while the expression is true.
An expression is true when its result is nonempty and
contains only nonzero elements (logical or real
numeric). Otherwise, the expression is false.
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
19
while Loop: Syntax
while <expression>
statements
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
20
while Loop: Example
num = input('Enter a number: ');
n=num;
f = n;
while n > 1
n = n-1;
f = f*n;
end
disp([num2str(num), '! = ', num2str(f)])
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
21
break Statement
break terminates the execution of a for or while loop.
Statements in the loop after the break statement do
not execute.
In nested loops, break exits only from the loop in
which it occurs. Control passes to the statement that
follows the end of that loop.
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
22
break Statement: Example
while true
x=randi(100,1);
if x<=60 && x>=40
break
end
disp(x);
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
23
continue Statement
continue passes control to the next iteration of a for
or while loop.
It skips any remaining statements in the body of the
loop for the current iteration.
The program continues execution from the next
iteration.
continue applies only to the body of the loop where
it is called.
In nested loops, continue skips remaining statements
only in the body of the loop in which it occurs.
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
24
continue Statement: Example
Remainder after
disp ('Odd Numbers:'); division (modulo
operation) using
for n = 1:10 mod function as %
is used to
if mod(n,2)==0 comment a line
continue
end
disp( num2str(n))
end
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
25
Assignments
1. Write a program to print the Fibonacci Series.
2. Write a program to display the sum of a digit.
3. Write a program to reverse a number.
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA
26
Susovan Jana, Department of Information Technology, IEM, Kolkata, INDIA