0% found this document useful (0 votes)
6 views18 pages

Matlab Lect 6

The document explains the use of IF statements and SWITCH CASE statements in MATLAB for decision-making. It provides syntax, examples, and flow diagrams for various conditional structures including nested IF statements and the SWITCH statement. The document also illustrates how to determine conditions such as even/odd numbers and comparing values using these control structures.

Uploaded by

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

Matlab Lect 6

The document explains the use of IF statements and SWITCH CASE statements in MATLAB for decision-making. It provides syntax, examples, and flow diagrams for various conditional structures including nested IF statements and the SWITCH statement. The document also illustrates how to determine conditions such as even/odd numbers and comparing values using these control structures.

Uploaded by

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

DECISION THROUGH IF

STATEMENT AND SWITCH


CASE STATEMENTS.

Week 6
IF STATEMENT
 If evaluates a logical expression and
executes a group of statements based on the
value of the expression.

 Syntax of If Statement
if expression
Statements
end
EXAMPLE
x = 10;
if x ~= 0
disp('Nonzero value')
end

Output:
Nonzero value
EXAMPLE
a = 100; b = 200;
if( a == 100 )
if( b == 200 )
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
x = 78;
if x >= 90
disp('A')
end
if x >= 80
disp(‘B')
end
if x >= 70
disp(‘C')
Output:
end B
if x >= 60
disp(‘D')
end
if x <60
disp(‘F')
end
MATLAB IF-ELSE... END STATEMENT
 If the first condition is not true, then we can
define other statements to run by using
the else keyword.
Syntax:
if expression
Statements
else
Statements
end
Flow Diagram of if...else...end Statement
EXAMPLE
% program to check the number is even or odd

a = randi(100,1);
if rem(a,2) == 0
disp('an even number')
else
disp('an odd number')
end

Output :
a = 15 an odd number
EXAMPLE
% Using if-else statement to determine the
largest of two number
num1 = input('Enter number 1: ');
num2 = input('Enter number 2: ');
if (num1 > num2)
disp('num1 is greater than num2')
else if (num1 < num2)
disp('num1 is less than num2')
else if (num1 == num2)
disp('num1 is equal to num2')
Output :
end Enter number 1: 15 Enter number 2:20 num1 is less than
num2
MATLAB IF-ELSEIF-ELSE...END STATEMENT

 If we have more than one option or condition


to check, then use elseif keyword.
Syntax:
if expression
Statements
elseif expression
Statements
else
Statements
end
FLOWCHART OF IF...ELSEIF... ELSE...END STATEMENTS
EXAMPLE
% program to compare two numbers
% generate random number for your age
n = randi(100,1);
age = 23;
% check the number is greater than your age
if n > age
disp('i am younger')
elseif n < age
disp('you are younger')
else Output:
n = 9 you are younger
disp('we are of same age')
end
MATLAB NESTED IF-ELSE

 If statements can be nested, but each if statement


requires the end keyword.
Syntax:
if expression
Statements
if expression
Statements
else
Statements
end
elseif expression
Statements
if expression
Statements
end
else
Statements
end
EXAMPLE
a = 100;
if a == 10
fprintf('Value of a is 10\n' );
elseif( a == 20 )
fprintf('Value of a is 20\n' );
elseif a == 30
fprintf('Value of a is 30\n' );
else
fprintf('None of the values are matching\n');
fprintf('Exact value of a is: %d\n', a );
end
OUTPUT: None of the values are matching Exact value of a is: 100
MATLAB SWITCH

 If we want to test the equality against a pre-


defined set of rules, then the switch statement
can be an alternative of the if statement.
Syntax:
switch switch_expression
case case_expression1
Statements
case case_expression2
Statements
case case_expressionN
Statements
otherwise
Statements
End
FLOWCHART OF SWITCH STATEMENT
EXAMPLE
% program to check whether the entered number is a weekday or not
a = input('enter a number : ')
switch a
case 1
disp('Monday')
case 2
disp('Tuesday')
case 3
Output:
disp('Wednesday') enter a number: 4 Thursday
case 4
disp('Thursday')
case 5
disp('Friday')
case 6
disp('Saturday')
case 7
disp('Sunday')
otherwise
disp('not a weekday')
end

You might also like