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

If Else Switch

Uploaded by

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

If Else Switch

Uploaded by

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

If-Else and Switch-Case in MATLAB

Introduction to If-Else Statements:

An 'if-else' statement is used for conditional execution of code blocks.

Syntax:

if condition

% Statements

elseif another_condition

% Statements

else

% Statements

end

Example:

x = 10;

if x > 5

disp('x is greater than 5');

else

disp('x is less than or equal to 5');

end
Switch-Case Statements:

The 'switch-case' statement is used to choose one block of code based on a variable's value.

Syntax:

switch variable

case value1

% Statements

case value2

% Statements

otherwise

% Statements

end

Example:

fruit = 'apple';

switch fruit

case 'apple'

disp('You chose an apple.');

case 'banana'

disp('You chose a banana.');

otherwise

disp('Unknown fruit.');

end

You might also like