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

Exercises Switch Case and Operators Matlab

The document contains examples of using the switch statement in MATLAB. Example 1 checks if a number is a weekday or not. Examples 2-5 demonstrate using operators with switch. Example 6 takes a grade as input and outputs a message using switch.

Uploaded by

talbkhfajy4
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)
60 views

Exercises Switch Case and Operators Matlab

The document contains examples of using the switch statement in MATLAB. Example 1 checks if a number is a weekday or not. Examples 2-5 demonstrate using operators with switch. Example 6 takes a grade as input and outputs a message using switch.

Uploaded by

talbkhfajy4
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

MATLAB 2022- 2023 ‫الجامعة التكنولوجية‬

‫المرحلة الثانية‬ ‫قسم تكنولوجيا النفط‬

Exercises Switch Case and operators


Example1:

% 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
disp('Wednesday')
case 4
disp('Thursday')
case 5
disp('Friday')
case 6
disp('Saturday')
case 7
disp('Sunday')
otherwise
disp('not a weekday')
end

1
MATLAB 2022- 2023 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم تكنولوجيا النفط‬

Example2: Example3:
x = 222, y = 3; x = 222, y = 3;
switch x switch x
case (x==y) case x*(x==y)
disp('x and y are equal'); disp('x and y are equal');
case (x>y) case x*(x>y)
disp('x is greater than y'); disp('x is greater than y');
otherwise otherwise
disp('x is less than y'); disp('x is less than y');
end end

Example4: Example5:
a=input('enter number'); a=input('enter number');
b=a;
switch true switch a

case a>80 case b*(a>80)


disp('great than 80');
disp('great than 80');
case a> 60
case b*(a> 60)
disp('great than 60');
case ( a> 30 && a< 60 ) disp('great than 60');
disp('great than 30'); case b* ( a> 30 && a< 60 )
otherwise disp('great than 30');
disp('error'); otherwise
disp('hell0');
end
end

2
MATLAB 2022- 2023 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم تكنولوجيا النفط‬

Example 6:
clc
clear
Entergrade=input('enter grade letter A,B,C,D,F or others ','S');

switch(Entergrade)
case 'A'
fprintf('Excellent performance!\n' );
case 'B'
fprintf('Well done performance\n' );
case 'C'
fprintf('Very Good performance\n' );
case 'D'
fprintf('You passed.. Congratulations\n' );
case 'F'
fprintf('Better luck next time\n' );
otherwise
fprintf('Invalid grade. Please enter correct value\n' );
end

Note:"switch" is not designed for classifying numbers in to ranges.

You might also like