Conditional Statements in MATLAB
Conditional statements allow you to execute specific sections of code based on logical conditions. They are essential for decision-making in programming and help control the flow of execution in MATLAB scripts and functions.
Overview of Conditional Statements
The key conditional statements in MATLAB include:
if
: Executes code when a condition is true.else
: Executes code when theif
condition is false.elseif
: Tests additional conditions if the previous ones are false.switch
: Selects execution based on multiple possible values of a variable.
Basic Syntax of if
Statement
The syntax for an if
statement is as follows:
% Example of if statement
x = 10;
if x > 5
disp('x is greater than 5');
end
disp
will output:
x is greater than 5
Using else
and elseif
The else
and elseif
statements extend the functionality of if
.
% Example with else and elseif
x = 3;
if x > 5
disp('x is greater than 5');
elseif x == 5
disp('x is equal to 5');
else
disp('x is less than 5');
end
For x = 3
, the output will be:
x is less than 5
The switch
Statement
The switch
statement is used when you have multiple conditions to check for a single variable.
% Example of switch statement
choice = 'B';
switch choice
case 'A'
disp('Choice is A');
case 'B'
disp('Choice is B');
case 'C'
disp('Choice is C');
otherwise
disp('Invalid choice');
end
For choice = 'B'
, the output will be:
Choice is B
Combining Conditions
You can combine multiple conditions using logical operators:
&&
: Logical AND||
: Logical OR~
: Logical NOT
% Example of combined conditions
x = 4;
y = 8;
if x > 3 && y < 10
disp('Both conditions are true');
end
The output will be:
Both conditions are true
Using Conditional Statements with Loops
Conditional statements can be used in conjunction with loops to handle repetitive tasks based on certain conditions. Here’s an example:
Example: Checking Odd or Even Numbers in a Range
% Example with for loop and if statement
for num = 1:10
if mod(num, 2) == 0
disp([num2str(num) ' is even']);
else
disp([num2str(num) ' is odd']);
end
end
The output will be:
1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even
Example: Nested Loops with Conditional Statements
Conditional statements can also be nested inside loops. Here’s an example of finding prime numbers:
% Example with nested loops and if statements
for num = 2:10
isPrime = true;
for divisor = 2:sqrt(num)
if mod(num, divisor) == 0
isPrime = false;
break;
end
end
if isPrime
disp([num2str(num) ' is a prime number']);
else
disp([num2str(num) ' is not a prime number']);
end
end
The output will identify which numbers between 2 and 10 are prime:
2 is a prime number 3 is a prime number 4 is not a prime number 5 is a prime number 6 is not a prime number 7 is a prime number 8 is not a prime number 9 is not a prime number 10 is not a prime number
Example: Breaking Out of Loops
Conditional statements can also be used to break out of loops:
% Example with while loop and break
count = 0;
while true
count = count + 1;
if count > 5
disp('Breaking the loop');
break;
else
disp(['Count is: ' num2str(count)]);
end
end
The output will be:
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Breaking the loop
Useful MATLAB Functions for Conditional Statements
Practice Questions
Test Yourself
1. Write a script that takes an input number and checks if it is positive, negative, or zero.
2. Create a switch
statement to print the day of the week based on an input number (1 for Monday, 2 for Tuesday, etc.).
3. Write a program to calculate the grade of a student based on their marks using conditional statements.