0% found this document useful (0 votes)
23 views5 pages

If Rand 0.5 Disp ( It Was Less Than .5!') Else Disp ( It Was Not Less Than .5!') End

The document contains examples of conditional statements in MATLAB code. It shows if/elseif/else statements to check conditions on input values and print corresponding outputs. It also shows a switch statement to check a grade input and print different messages based on the grade.

Uploaded by

somit pandey
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)
23 views5 pages

If Rand 0.5 Disp ( It Was Less Than .5!') Else Disp ( It Was Not Less Than .5!') End

The document contains examples of conditional statements in MATLAB code. It shows if/elseif/else statements to check conditions on input values and print corresponding outputs. It also shows a switch statement to check a grade input and print different messages based on the grade.

Uploaded by

somit pandey
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/ 5

if rand < 0.

5
disp(‘It was less than .5!’)
else
disp(‘It was not less than .5!’)
end
a =input('value of a:');
b = input('value of b:');
% 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 );
a = 100;
%check the boolean condition
if a == 10
% if condition is true then print the following
fprintf('Value of a is 10\n' );
elseif( a == 20 )
% if else if condition is true
fprintf('Value of a is 20\n' );
elseif a == 30
% if else if condition is true
fprintf('Value of a is 30\n' );
else
% if none of the conditions is true
fprintf('None of the values are matching\n');
fprintf('Exact value of a is: %d\n', a );
end
switch <switch_expression>
case <case_expression>
<statements>
case <case_expression>
<statements> ... ...
otherwise <statements>
end
grade = input('grade : ');
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

You might also like