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

Lecture 4

The document provides an overview of the 'if' statement in MATLAB, detailing its syntax and various forms including basic if, if-else, if-elseif-else, and nested if statements. It also explains how to combine conditions using logical operators and highlights common mistakes to avoid. Additionally, the document includes practical examples demonstrating the use of these conditional statements.

Uploaded by

Hiba Al-Qaisy
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)
3 views

Lecture 4

The document provides an overview of the 'if' statement in MATLAB, detailing its syntax and various forms including basic if, if-else, if-elseif-else, and nested if statements. It also explains how to combine conditions using logical operators and highlights common mistakes to avoid. Additionally, the document includes practical examples demonstrating the use of these conditional statements.

Uploaded by

Hiba Al-Qaisy
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/ 6

if Condition Basics in MATLAB

The if statement in MATLAB allows you to execute code only when a certain condition
is true. It is a fundamental part of controlling program flow.

1. Basic if Statement Syntax


if condition
% Code to execute if condition is true
end

Example:
x = 5;
if x > 0
disp('x is positive');
end

Output:

x is positive

2. if-else Statement
If the condition is false, the else block runs.

if condition
% Code if true
else
% Code if false
end

Example:
x = -3;
if x > 0
disp('x is positive');
else
disp('x is not positive');
end

Output:

x is not positive

3. if-elseif-else (Multiple Conditions)


Checks multiple conditions sequentially.

if condition1
% Code if condition1 is true
elseif condition2
% Code if condition2 is true
else
% Code if all conditions are false
end

Example:
x = 0;
if x > 0
disp('x is positive');
elseif x < 0
disp('x is negative');
else
disp('x is zero');
end

Output:

x is zero
4. Nested if Statements
You can place if statements inside other if blocks.

if condition1
if condition2
% Code if both conditions are true
end
end

Example:
x = 10;
y = 5;
if x > 0
if y > 0
disp('Both x and y are positive');
end
end

Output:

Both x and y are positive

5. Combining Conditions (&&, ||, ~)


• && (AND) → Both conditions must be true.

• || (OR) → At least one condition must be true.

• ~ (NOT) → Inverts the condition.

Example:
x = 4;
if (x > 2) && (x < 6)
disp('x is between 2 and 6');
end

Output:
x is between 2 and 6

6. Common Mistakes
❌ Using = instead of ==

• = is for assignment, == checks equality.


x = 5;
if x == 5 % Correct (comparison)
disp('x is 5');
end

❌ Missing end

• Every if must be closed with end.

Lab Examples:

A = [1 2 3;
4 5 6;
7 8 9];
%[] delete
A(end, :) = []
A = [1 2 3;
4 5 6;
7 8 9];

indices = find(A > 2 & A < 6)


disp(indices);
[row, col] = find(A > 2 & A < 6);
disp([row, col]); % Displays pairs of (row, column) indices
Z=A.^2

x= 5;
if x > 0
disp('x is positive');
disp('x is positive');
disp('x is positive');
disp('x is positive');

end
x=5
if x == 5 % Correct (comparison)
disp('x is 5');
end

x = 2;
if (x > 2) && (x < 6)
disp('x is between 2 and 6');
end

x = -3;
if x > 0
disp('x is positive');
else
disp('x is not positive');
end

x = 0;
if x > 0
disp('x is positive');
elseif x < 0
disp('x is negative');
else
disp('x is zero');
end

x = 10;
y = 5;
if x > 0
if y > 0
disp('Both x and y are positive');
end
end

A = [1, 5, 3, 8];
if any(A > 6) % Checks if any element is > 6
disp('At least one element is greater than 6');
end

B = [2, 4, 5];
if all(B < 6) % Checks if all elements are < 6
disp('All elements are less than 6');
end

v=2:8
if v(mod(v,2)==0)
disp("even")
disp(v(mod(v,2)==0))
end

You might also like