Practical Work N 1
Practical Work N 1
Faculty of Sciences
Department of Computer Science
L2 Computer Science
2024-2025
Practical Work “01”
I. The MATLAB environment. II. Basic syntax.
V.3 if…elseif…elseif…else…end
Example
Syntax In the script editor window, type:
if <condition 1>
Commands
Executes when expression 1 is true.
grade = 'B';
elseif <condition 2>
Executes when expression 2 is true. switch(grade)
elseif <condition 3> case 'A'
Executes when expression 3 is true. fprintf ('Excellent! \n’);
else case 'B'
Executes when none of the conditions are true. fprintf ('Well done \n');
end case 'C'
fprintf ('Well done \n');
case 'D'
Example fprintf ('You passed \n');
In the script editor window, type: case 'F'
Commands fprintf ('Better try again \n');
a = 100; otherwise
if a == 10 fprintf ('Invalid grade \n');
fprintf ('the value of a is 10\n’); end
elseif(a==20) Results
fprintf ('the value of a is 20\n’); Well done
elseif a == 30
fprintf ('the value of a is 30\n’);
else
fprintf ('None of the values match \n');
fprintf ('The exact value is: %d\n', a);
end
Results
None of the values match
The exact value is: 100