Conditional statements
Decision making structures require that the
programmer specify one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the
condition is determined to be true, and optionally,
other statements to be executed if the condition is
determined to be false.
Following is the general form of a typical decision
making structure found in most of the programming
languages:
Only If condition
a = 10;
if a < 20
fprintf('a is less than 20\n' );
end
fprintf('value of a is : %d\n', a);
if and else condition
a = 100;
if a < 20
fprintf('a is less than 20\n' );
else
fprintf('a is not less than 20\n' );
end
fprintf('value of a is : %d\n', a);
Write a program to find weather the number
is even or odd
X = 20;
Y = 2;
R = rem(X,Y)
if R==0
{
fprintf('true' );
}
else
{
fprintf('false' );
}
end
write a program to find weather the no is divisible By 7 or not?
write a prog to find weather the boy is able to vote or note:
Nested If else statements
It is always legal in MATLAB to nest if-else
statements which means you can use one if or elseif
statement inside another if or elseif statement(s).
Syntax:
The syntax for a nested if statement is as follows:
if <expression 1>
% Executes when the boolean expression 1 is true
if <expression 2>
% Executes when the boolean expression 2 is true
end
end
Example:
Create a script file and type the following code in it:
a = 100;
b = 200;
% check the boolean condition
if( a == 100 )
% if condition is true then check the following
if( b == 200 )
% if condition is true then print the following
fprintf('Value of a is 100 and b is 200\n' );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );
output:
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
Example
a = 400;
b = 50;
c= 300;
% check the boolean condition
if( a > b )
% if condition is true then check the following
if( a > c )
% if condition is true then print the following
fprintf('a' );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );
write a program to find greater between 3 numbers
a = 1000;
b = 22200;
c= 3000;
% check the boolean condition
if( a > b )
if( a > c )
fprintf('a' );
else
fprintf('c' );
end
else
if( b > c )
fprintf('b' );
else
fprintf('c' );
end
end
Switch case
A switch block conditionally executes one set of statements
from several choices. Each choice is covered by a case
statement.
An evaluated switch_expression is a scalar or string.
An evaluated case_expression is a scalar, a string or a cell
array of scalars or strings.
The switch block tests each case until one of the cases is
true. A case is true when:
For a cell array case_expression, at least one of the
elements of the cell array matches switch_expression, as
defined above for numbers, strings and objects.
When a case is true, MATLAB executes the corresponding
statements and then exits the switch block.
Syntax
The syntax of switch statement in MATLAB is:
switch <switch_expression>
case <case_expression>
<statements>
case <case_expression>
<statements>
...
...
otherwise
<statements>
end
grade = 'A';
switch(grade)
case 'A'
fprintf('Excellent!\n' );
case 'B'
fprintf('Well done\n' );
case 'C'
fprintf('Well done\n' );
case 'D'
fprintf('You passed\n' );
case 'F'
fprintf('Better try again\n' );
otherwise
fprintf('Invalid grade\n' );
end
nested Switch case
Syntax:
The syntax for a nested switch statement is as follows:
switch(ch1)
case 'A'
fprintf('This A is part of outer switch');
switch(ch2)
case 'A'
fprintf('This A is part of inner switch' );
case 'B'
fprintf('This B is part of inner switch' );
end
case 'B'
fprintf('This B is part of outer switch' );
end
Example:
Create a script file and type the following code in it:
a = 100;
b = 200;
switch(a)
case 100
fprintf('This is part of outer switch %d\n', a );
switch(b)
case 200
fprintf('This is part of inner switch %d\n', a );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );